4

We need to get an instance of a service inside of the Configure() method in Startup.cs for an Azure Function app on .NET 6. For .NET core apps, this can be achieved by adding the service inside the ConfigureServices() method, and then getting the required service in the Configure().

However, in a .NET function app, there isn't a separation between Configure() and ConfigureServices() - there's only one method, Configure().

How do we both register and then get an instance of a service in this method? If we try:

public override void Configure(IFunctionsHostBuilder builder)
{
    builder.Services.AddHttpClient();
    var provider = builder.Services.BuildServiceProvider();
    var clientFactory = provider.GetService<IHttpClientFactory>();
}

Then, using clientFactory anywhere throws an error Value cannot be null. (Parameter 'provider'). Is there a way to "wait" for all services to be built, then get an instance of it?

Note: We are specifically looking for a way to do this in .NET functions. All the existing questions currently are for .NET core.

2
  • Please refer Register Services in Azure Functions Commented Mar 14, 2022 at 19:00
  • 2
    @HarshithaVeeramalla-MT But how do I get an instance of the service that was registered? Using GetService from BuildServiceProvider doesn't seem to work very well
    – a3y3
    Commented Mar 14, 2022 at 20:09

0

Browse other questions tagged or ask your own question.