Unable to resolve service for type 'Microsoft.Extensions.Logging.ILoggerFactory' in .net Core 3.0
See original GitHub issueDescribe the bug
Got error Unable to resolve service for type ‘Microsoft.Extensions.Logging.ILoggerFactory’ when using public Startup(IConfiguration configuration, ILoggerFactory loggerFactory)
To Reproduce
Steps to reproduce the behavior:
- Using this version of ASP.NET Core ‘.Net Core SDK 3.0.100-preview2’ and VS 2019 Preview
- Create a new Asp.Net Core MVC Template with .net core 3.0
- Modify
Startup.cs
with
public class Startup
{
private readonly ILogger<Startup> _logger;
public Startup(IConfiguration configuration, ILoggerFactory loggerFactory)
{
Configuration = configuration;
_logger = loggerFactory.CreateLogger<Startup>();
}
- Run project and got error
An error occurred while starting the application. InvalidOperationException: Unable to resolve service for type ‘Microsoft.Extensions.Logging.ILoggerFactory’ while attempting to activate ‘WebApplication21.Startup’. Microsoft.Extensions.DependencyInjection.ActivatorUtilities+ConstructorMatcher.CreateInstance(IServiceProvider provider)
InvalidOperationException: Unable to resolve service for type ‘Microsoft.Extensions.Logging.ILoggerFactory’ while attempting to activate ‘WebApplication21.Startup’. Microsoft.Extensions.DependencyInjection.ActivatorUtilities+ConstructorMatcher.CreateInstance(IServiceProvider provider) Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, object[] parameters) Microsoft.AspNetCore.Hosting.Internal.GenericWebHostBuilder.UseStartup(Type startupType, HostBuilderContext context, IServiceCollection services) Microsoft.AspNetCore.Hosting.Internal.GenericWebHostBuilder+<>c__DisplayClass13_0.<UseStartup>b__2(IApplicationBuilder app) Microsoft.AspNetCore.Server.IIS.Core.IISServerSetupFilter+<>c__DisplayClass2_0.<Configure>b__0(IApplicationBuilder app) Microsoft.AspNetCore.HostFilteringStartupFilter+<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app) Microsoft.AspNetCore.Hosting.Internal.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
Expected behavior
Successfully resove service ILoggerFactory loggerFactory
Issue Analytics
- State:
- Created 5 years ago
- Reactions:13
- Comments:15 (3 by maintainers)
Top GitHub Comments
Yes, in 3.0 we made a change to limit the types you can inject in to
Startup
. It was a misleading feature because it used a completely separate DI container to the rest of the app. You can still injectILogger<T>
in to yourConfigure
method directly, but no longer in to the constructor orConfigureServices
. (cc @Tratcher)See https://github.com/aspnet/Announcements/issues/353 for more details and https://github.com/aspnet/AspNetCore/issues/9337 for discussions on this topic.
Those aren’t our docs but I submitted a PR to update it: https://github.com/MicrosoftDocs/azure-docs/pull/33059 . Feel free to suggest edits for any other docs that are inconsistent here!