简体   繁体   English

ASP.NET 核心WebJob DI

[英]ASP.NET Core WebJob DI

I would like to use DI in my WebJobs same way I use in WebApps but honestly I don't know how Functions.cs is called.我想在我的 WebJobs 中使用 DI,就像在 WebApps 中使用一样,但老实说,我不知道 Functions.cs 是如何被调用的。 If I insert a Console.WriteLine inside Functions.cs's constructor it is not printed.如果我在 Functions.cs 的构造函数中插入 Console.WriteLine,则不会打印。

How could I make this work?我怎样才能使这项工作?

Program.cs程序.cs

class Program
  {
    static void Main(string[] args)
    {
      var builder = new HostBuilder();

      builder.ConfigureWebJobs(b =>
      {
        b.AddAzureStorageCoreServices();
        b.AddAzureStorage();
      });

      builder.ConfigureLogging((context, b) => {
        b.AddConsole();
      });

      builder.ConfigureServices((context, services) => {
        // Inject config
        services.Configure<Secrets.ConnectionStrings>(context.Configuration.GetSection("ConnectionStrings"));
        services.AddSingleton<Functions>();
        services.AddSingleton<MyEmail>();
        services.AddSingleton<IMyFunc, MyFunc>();

        services.BuildServiceProvider();
      });

      var host = builder.Build();

      using (host)
      {
        host.Run();
      }
    }
  }

Functions.cs函数.cs

public class Functions
  {
    private static IOptions<Secrets.ConnectionStrings> _myConnStr;
    private static MyEmail _myEmail;
    private static IMyFunc _myFunc;

    public Functions(IOptions<Secrets.ConnectionStrings> ConnectionString, MyEmail MyEmail, MyFunc MyFunc)
    {
      _myConnStr = ConnectionString;
      _myEmail = MyEmail;
      _myFunc = MyFunc;

      Console.WriteLine("Functions constructor");
    }


    public static void ProcessQueueMessage
    (
      [QueueTrigger("teste")]
      string message,
      ILogger logger
    )
    {
      // use my injected stuff
    }
  }

Thank you very much.非常感谢。

Azure fuctions work a bit different than you are used to. Azure 功能的工作方式与您习惯的有所不同。 You can do something like the following below:您可以执行以下操作:

using System;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Http;
using Microsoft.Extensions.Logging;

[assembly: FunctionsStartup(typeof(MyNamespace.Startup))]

namespace MyNamespace
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddHttpClient();

            builder.Services.AddSingleton((s) => {
                return new MyService();
            });

            builder.Services.AddSingleton<ILoggerProvider, MyLoggerProvider>();
        }
    }
}

You can read a lot more on Microsoft homepage here: https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection您可以在此处的 Microsoft 主页上阅读更多内容: https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection

If using dependency injection via constructor with your function then the function itself needs to be an instance member and not static member.如果通过构造函数对 function 使用依赖注入,则 function 本身需要是实例成员,而不是 static 成员。

Your constructor is not being called in your example because the function is static and is thus there is no need to call the instance constructor您的示例中没有调用您的构造函数,因为 function 是 static,因此无需调用实例构造函数

public class Functions {
    private readonly IOptions<Secrets.ConnectionStrings> connectionStrings;
    private readonly MyEmail myEmail;
    private readonly IMyFunc myFunc;

    public Functions(IOptions<Secrets.ConnectionStrings> connectionStrings, MyEmail myEmail, MyFunc MyFunc) {
        this.connectionString = connectionStrings;
        this.myEmail = myEmail;
        this.myFunc = myFunc;

        Console.WriteLine("Functions constructor");
    }

    public void ProcessQueueMessage(
        [QueueTrigger("teste")]
        string message,
        ILogger logger
    ) {
        // use my injected stuff
    }
}

From there it is just a matter of following the details from documentation从那里只需遵循文档中的详细信息

Reference Use dependency injection in .NET Azure Functions参考在 .NET Azure 函数中使用依赖注入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM