Webjobs 方法索引 - 依赖注入异常 IConfiguration

Webjobs Method Indexing - Dependency Injection Exception IConfiguration

.NET Core 2.2, WebJobs SDK v3.0

我这样初始化主机

        HostBuilder builder = new HostBuilder();
        builder.ConfigureAppConfiguration((context, configApp) => {
            configApp.AddJsonFile("appsettings.json", optional: true);
            configApp.AddJsonFile(
               $"appsettings.{context.HostingEnvironment.EnvironmentName}.json",
               optional: true);
        });

        builder.ConfigureWebJobs((context, configure) => {
            configure.AddAzureStorage();
            configure.Services.AddSingleton(context.Configuration);
        });

        builder.ConfigureServices((context, services) => {
            services.AddSingleton(context.Configuration);
        });

我的 webJob 函数处理队列中的消息如下所示:

public static void ProcessQueueMessage([QueueTrigger("queuename")] CloudQueueMessage queueMessage, 
  TextWriter log, IConfiguration configuration)

当我的主机启动时出现异常

Microsoft.Azure.WebJobs.Host.Indexers.FunctionIndexingException: 'Error indexing method 'Functions.ProcessQueueMessage''

InvalidOperationException: Cannot bind parameter 'configuration' to type IConfiguration. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).

我做错了什么?

您可以使用完整的 class 和构造函数注入,而不是使用静态函数和参数注入。


services.AddScoped<Functions>()
services.AddSingleton(context.Configuration);

...

public class Functions
{
    private readonly IConfiguration _Configuration;

    public Functions(IConfiguration configuration)
    {
        _Configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
    }

    public void ProcessQueueMessage([QueueTrigger("queuename")] CloudQueueMessage queueMessage, 
      TextWriter log)
    {
       //
    }
}