Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CreateDefaultBuilder & ConfigureWebHost/ConfigureWebHostDefaults are broken #14928

Closed
Kaelum opened this issue Oct 8, 2019 — with docs.microsoft.com · 2 comments
Closed
Labels
Source - Docs.ms Docs Customer feedback via GitHub Issue

Comments

Copy link

Kaelum commented Oct 8, 2019

Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});

The above code is being using in multiple places, but it fails if the StartUp class constructor contains something like "ILogger<Startup> logger" in addition to the "IConfiguration configuration". If you don't use the "Host.CreateDefaultBuilder()", and instead start with a WebHostBuilder object, everything works as expected. I am using the following constructor:

public Startup(ILogger<Startup> logger, IConfiguration configuration)
{
Logger = logger;
Configuration = configuration;
}

Which works perfectly when starting with a WebHostBuilder object, but fails with the following exception when starting with the HostBuilder objects:

System.InvalidOperationException: Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger`1[Webroot.Bcap.WebApi.Startup]' while attempting to activate 'Webroot.Bcap.WebApi.Startup'.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@dotnet-bot dotnet-bot added the Source - Docs.ms Docs Customer feedback via GitHub Issue label Oct 8, 2019
@guardrex
Copy link
Collaborator

guardrex commented Oct 8, 2019

That's correct. That's the new 3.0 behavior. See 👉 aspnet/Announcements#353.

Inject your logger directly into Startup.Configure ...

https://docs.microsoft.com/aspnet/core/fundamentals/logging/#create-logs-in-the-startup-class

If you need it in Startup.ConfigureServices, call GetRequiredService for an instance ...

var logger = services.GetRequiredService<ILogger<Program>>();

@guardrex guardrex closed this as completed Oct 8, 2019
@Kaelum
Copy link
Author

Kaelum commented Oct 8, 2019

For those who see this after the issue was closed, here is a link to the thread where they try to defend this change, but fail miserably:

[Announcement] Generic Host restricts Startup constructor injection

If you are using NLog, you can use the following line as a work-around for this ridiculous issue:

		Logger = LogManager.GetCurrentClassLogger();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Source - Docs.ms Docs Customer feedback via GitHub Issue
Projects
None yet
Development

No branches or pull requests

3 participants