Azure / Azure/azure-functions-host
Requiring ILogger<> during function startup throws InvalidOperationException
- Dominant language
- C#
- Stars
- 2k
- Forks
- 482
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 36
Description
This is a re-create of issue #5912.
I would like to initialize some caching code within an azure function. This caching service injects an ILogger<> which cannot be injected apparently, since not all required services have been registered.
When running the following line of code (part of the code below). i get the following exception:
`scope.ServiceProvider.GetRequiredService().Initialize();`
```
An exception of type 'System.InvalidOperationException' occurred in Microsoft.Extensions.DependencyInjection.dll but was not handled in user code
Unable to resolve service for type 'Microsoft.Azure.WebJobs.Script.IFileLoggingStatusManager' while attempting to activate 'Microsoft.Azure.WebJobs.Script.Diagnostics.HostFileLoggerProvider'.
```
#### Investigative information
- Function App version: v3 (Microsoft.NET.Sdk.Functions" Version="3.0.6")
- dot net core 3.1
Startup code:
```
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddLogging();
RegisterServices(builder.Services);
var serviceProvider = builder.Services.BuildServiceProvider(true);
InitializeServices(serviceProvider);
}
private static void RegisterServices(IServiceCollection services)
{
var serviceProvider = services.BuildServiceProvider();
services.AddSingleton();
}
private static void InitializeServices(IServiceProvider serviceProvider)
{
using var scope = serviceProvider.CreateScope();
scope.ServiceProvider.GetRequiredService().Initialize().Wait();
}
}
```
Caching service:
```
public interface ICachingService
{
Task Initialize();
}
public class CachingService : ICachingService
{
private readonly ILogger _logger;
public CachingService(ILogger logger)
{
_logger = logger;
}
public async Task Initialize()
{
_logger.LogInformation("initialization");
await Task.CompletedTask;
}
}
```
The workaround would be:
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton(p =>
{
var logger = p.GetService>();
var cachingService = new CachingService(logger);
cachingService.Initialize().Wait();
return cachingService;
});
}
It seems the ilogger dependencies are registered too late. In my opinion these should be logged before Configure is called.
Contributor guide
Research direction
Start by reproducing the Functions v3/.NET Core 3.1 startup code and trace Configure, BuildServiceProvider, and resolution of ICachingService with ILogger. Follow the registration path for IFileLoggingStatusManager and HostFileLoggerProvider; done means the reported startup resolution failure is resolved or its supported initialization sequence is clearly established.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- azure, csharp
- Domain
- backend, cloud
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100