我在使用来注册单例时遇到问题Microsoft.Extensions.DependencyInjection。我创建了一个Startup类(肯定可以正常工作)并创建了一个ITestServicewith实现。
单例被注入到函数的构造函数中。最初,我对此实现没有问题,但是几天后,该函数由于无法解析而失败ITestService。我不知道为什么。
这是Startup班
using Microsoft.Azure.WebJobs.Hosting;
[assembly: WebJobsStartup(typeof(Startup))]
namespace Test.Functions
{
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
public class Startup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("local.settings.json", true, true)
.AddEnvironmentVariables()
.Build();
builder.Services.AddSingleton<ITestService>(new TestService("test string"));
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是功能
public class TestFunction
{
private readonly ITestService testService
public TestFunction(ITestService testService)
{
this.testService = testService ?? throw new ArgumentNullException(nameof(testService));
}
[FunctionName(nameof(TestFunction))]
public void …Run Code Online (Sandbox Code Playgroud)