Reputation: 87037
Given a File->New->Azure Functions v2 App, I'm trying to get a reference to either an ILoggerFactory
or an ILogger<T>
.
I'm doing this in the StartUp.cs
class which is ran on the function app start.
Given the following code a weird exception is thrown:
var serviceProvider = builder.Services.BuildServiceProvider();
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
with the following exception:
A host error has occurred
[27/02/2019 8:21:22 AM] Microsoft.Extensions.DependencyInjection: Unable to resolve service for type 'Microsoft.Azure.WebJobs.Script.IFileLoggingStatusManager' while attempting to activate 'Microsoft.Azure.WebJobs.Script.Diagnostics.HostFileLoggerProvider'.
Value cannot be null.
Parameter name: provider
What is going on?
The full test repo/code can be found here on GitHub.
Upvotes: 15
Views: 8941
Reputation: 12371
This doesn't work, at least not yet. (link)
The services getting registered at startup are not fully ready to be used inside the Configure method itself. Read the section 'Caveats' here: https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection
"The startup class is meant for only setup and registration. Avoid using services registered at startup during the startup process. For instance, don't try to log a message in a logger that is being registered during startup. This point of the registration process is too early for your services to be available for use. After the Configure method is run, the Functions runtime continues to register additional dependencies, which can affect how your services operate."
Upvotes: 0
Reputation: 89
I've been looking for a solution for a while then I end up creating another service container for the startup class, but it'll need to register the required services to this container.
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
var services = builder.Services;
IConfiguration configuration = services.BuildServiceProvider().GetService<IConfiguration>();
var tempServiceContainer = new ServiceCollection()
.AddScoped(_ => configuration);
tempServiceContainer.AddSingleton<IYourService, YourService>();
}
}
Or maybe you could try using lazy Dependency Injection, but I haven't test it yet on the cloud server.
service.AddSingleton<IYourService, YourService>();
service.AddSingleton(provider => new Lazy<IYourService>(provider.GetService<IYourService>));
var yourService = serviceProvider.GetService<Lazy<IYourService>>();
Upvotes: 0
Reputation: 46621
It seems that some infrastructure (e.g. IFileLoggingStatusManager
) necessary for HostFileLoggerProvider
is not set up yet at the time you are creating the dependency injection container and attempt to resolve the logger in the StartUp class. I think you should delay logging until after the application has fully started.
If you look at the startup code of WebJobs you'll see that the logger gets added first, after that the external startup gets executed and finally the required logging services gets added. Which is the wrong order for your case.
Upvotes: 7