2d1b
2d1b

Reputation: 625

Webjobs Method Indexing - Dependency Injection Exception IConfiguration

.NET Core 2.2, WebJobs SDK v3.0

I initialize my host like this

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

My webJob function to process messages in a queue looks like this :

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

When my host starts I get an exception

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

What am I doing wrong?

Upvotes: 1

Views: 714

Answers (1)

Alex
Alex

Reputation: 18526

Instead of using static functions and parameter injection, you can use a full class and constructor injection.


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)
    {
       //
    }
}

Upvotes: 3

Related Questions