ASP.NET Core WebJob DI

时间:2019-11-19 19:28:30

标签: c# asp.net-core dependency-injection azure-webjobs

我想像在WebApps中一样在WebJobs中使用DI,但是老实说,我不知道如何调用Functions.cs。如果我在Functions.cs的构造函数中插入Console.WriteLine,则不会打印出来。

我该如何做?

Program.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

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
    }
  }

非常感谢您。

2 个答案:

答案 0 :(得分:1)

天蓝色功能的工作方式与您习惯的有所不同。您可以执行以下操作:

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>();
        }
    }
}

您可以在Microsoft主页上找到更多信息:https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection

答案 1 :(得分:1)

如果通过函数使用构造函数进行依赖注入,则函数本身必须是实例成员,而不是静态成员。

在您的示例中未调用您的构造函数,因为该函数是静态的,因此无需调用实例构造函数

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
    }
}

从那里开始,只需遵循文档中的详细信息即可

参考Use dependency injection in .NET Azure Functions