.NET Core 2.2 Azure Function v2 Dependency Injection

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.

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. You can install that explicitly with the following:

npm i -g [email protected]

See this GitHub issue for more background.


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.

EDIT :

Try Changing your code to the below and installing 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.

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

}