[英].NET Core 2.2 Azure Function v2 Dependency Injection
I am having an issue registering a singleton using Microsoft.Extensions.DependencyInjection
. 我在使用Microsoft.Extensions.DependencyInjection
注册单例时遇到问题。 I have created a Startup
class (which is definitely working) and created an ITestService
with implementation. 我创建了一个Startup
类(肯定有效)并创建了一个带有实现的ITestService
。
The singleton is injected into the function's constructor. 单例被注入函数的构造函数中。 Initially I had no issues with this implementation, however, a couple days later the function fails because it can't resolve ITestService
. 最初我对此实现没有任何问题,但是,几天后该函数失败,因为它无法解析ITestService
。 I have no clue why. 我不知道为什么。
here is the Startup
class 这是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"));
}
}
}
And here is the function 这是功能
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([ServiceBusTrigger("test", Connection = "ServiceBusConnection")]Message message, ILogger log)
{
////use test service here
}
}
When I debug startup and look at Services
I see that the implementation type is null
for ITestService
, which I assume is why it won't resolve. 当我调试启动并查看Services
我发现ITestService
的实现类型为null
,我认为这是无法解决的原因。 Like I mentioned this totally worked for a couple days. 就像我提到的,这完全工作了几天。 The version of functions etc have not changed. 功能等的版本没有改变。 Any ideas how to get this working again would be greatly appreciated. 任何想法如何让这个再次工作将不胜感激。
update 更新
I tried to simplify this even further and created another dummy interface with an implementation that has a parameter-less constructor. 我试图进一步简化这个并创建另一个虚拟接口,其实现具有无参数构造函数。 I added it using: builder.AddSingleton<ITestService2, TestService2>()
我使用: builder.AddSingleton<ITestService2, TestService2>()
添加它
It obviously assigned the type to the implementation type, but when it came time to inject it into the constructor it failed with the same can't activate exception. 它显然将类型分配给实现类型,但是当它将它注入构造函数时,它失败并且同样无法激活异常。
There's a regression in the latest version of the function host that has broken Dependency Injection. 最新版本的功能主机已经破坏了依赖注入。
In order to work around this in an Azure environment, you can lock down the specific version of the functions host by setting the FUNCTIONS_EXTENSION_VERSION
application setting to 2.0.12342.0
. 若要在Azure环境中解决此问题,可以通过将FUNCTIONS_EXTENSION_VERSION
应用程序设置为2.0.12342.0
来锁定功能主机的特定版本。
If you're running the function host locally using the azure-functions-core-tools
NPM package, be sure to use 2.4.419
as the latest version (2.4.498) results in the same issue. 如果您使用azure-functions-core-tools
NPM软件包在本地运行功能主机,请务必使用2.4.419
作为最新版本(2.4.498)导致相同的问题。 You can install that explicitly with the following: 您可以使用以下内容显式安装:
npm i -g azure-functions-core-tools@2.4.419
See this GitHub issue for more background. 有关更多背景信息,请参阅此GitHub问题 。
Try this in your code: 在您的代码中尝试此操作:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ITestService, TestService>();
}
Have a go with this 跟上这个吧
builder.Services.AddSingleton<ITestService>(s => new TestService("test string"));
This uses the IServiceProvider
in order to provide the string
parameter to the constructor. 这使用IServiceProvider
来为构造函数提供string
参数。
EDIT : 编辑:
Try Changing your code to the below and installing Willezone.Azure.WebJobs.Extensions.DependencyInjection 尝试将代码更改为以下内容并安装Willezone.Azure.WebJobs.Extensions.DependencyInjection
This adds the extension method AddDependencyInjection
and allows you to do the traditional ConfigureServices
method call in a net core app startup. 这会添加扩展方法AddDependencyInjection
并允许您在net core app startup中执行传统的ConfigureServices
方法调用。
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()
.AddDependencyInjection(ConfigureServices)
.Build();
}
private void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ITestService>(s => new TestService("test string"));
}
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.