简体   繁体   English

.NET Core 2.2 Azure功能v2依赖注入

[英].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.

相关问题 NET Core在Azure Function v2上进行EF Core迁移的问题 - Problem with EF Core migration on Azure Function v2 on .NET Core 通过 .Net Core 2.2、Azure Functions 实现的装饰器模式的依赖注入 - Dependency injection for Decorator pattern with multiple implementation via .Net Core 2.2, Azure Functions 如何在 asp.net core 2.2 中为依赖注入注册 IFileProvider? - How to register IFileProvider for the dependency injection in asp.net core 2.2? 如何在.net Core 2.2控制台应用程序中实现依赖项注入 - How to implement dependency injection in .net core 2.2 console application 如何在.net Core 2.2中使用依赖项注入来添加IEndpointsOperations接口 - How to add IEndpointsOperations interface using dependency injection in .net core 2.2 在 Azure Function v2 (.NET Core) 应用程序中读取 .NET Fx app.config - Reading .NET Fx app.config in an Azure Function v2 (.NET Core) app .NET Core 中的依赖注入 - Dependency injection in .NET Core 依赖注入无法解析在 Azure 中运行的 .NET Core Function App 中的类型的服务 - Dependency Injection unable to resolve service for type in .NET Core Function App running in Azure 如何在 Azure 函数(v2、.net 核心)中注册触发器绑定? - How do I register trigger bindings in an Azure Function (v2, .net core)? 如何在Azure Function v2(核心)中静态使用ConfigurationBuilder? - How to use ConfigurationBuilder staticly in an Azure Function v2 (core)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM