question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Announcement] Generic Host restricts Startup constructor injection

See original GitHub issue

TLDR: The only types the generic Host supports for Startup constructor injection are IHostEnvironment, IWebHostEnvironment, and IConfiguration. Applications using WebHost are unaffected.

In 3.0 we’ve re-platformed the web stack onto the generic host library. You can see the change in Program.cs in the templates:

2.x: https://github.com/aspnet/AspNetCore/blob/5cb615fcbe8559e49042e93394008077e30454c0/src/Templating/src/Microsoft.DotNet.Web.ProjectTemplates/content/EmptyWeb-CSharp/Program.cs#L20-L22 3.0: https://github.com/aspnet/AspNetCore/blob/b1ca2c1155da3920f0df5108b9fedbe82efaa11c/src/ProjectTemplates/Web.ProjectTemplates/content/EmptyWeb-CSharp/Program.cs#L19-L24

One key behavior change here is that Host only uses one dependency injection container to build the application, as opposed to WebHost that used one for the host and one for the app. As a result the Startup constructor no longer supports custom service injection, only IHostEnvironment, IWebHostEnvironment, and IConfiguration can be injected. This change was made to avoid DI issues such as duplicate singleton services getting created.

Mitigations:

Inject services into Startup.Configure: public void Configure(IApplicationBuilder app, IOptions<MyOptions> options)

[We’ll add more based on requests for specific scenarios.]

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:14
  • Comments:55 (16 by maintainers)

github_iconTop GitHub Comments

21reactions
grecosoftcommented, Jun 19, 2019

Ok. Seems to be limiting. Just seems that logging is so important that it should be created up front and allowed to be used anywhere. Is it possible to create a logger to be used during configuration?

20reactions
dustinlacewellcommented, Jul 7, 2019

This is a real disappointment. By using the WebHost container, I was able to register a number of classes as “Binding Services” or “Startup Services” via attributes. Then I could use IEnumerable<IStartupBinder> and IEnumerable<IStartupService> in ConfigureServices and Configure respectively and my startup looked like this:

    public class Startup {

        private ILogger<Startup> _logger { get; set; }
        private IEnumerable<IStartupBinder> _binders { get; }


        public Startup(IConfiguration config, ILogger<Startup> logger, IEnumerable<IStartupBinder> binders) {
            _logger = logger;
            _binders = binders;
        }

        public void ConfigureServices(IServiceCollection services) {
            foreach (var startupBinder in _binders) {
                _logger.LogInformation($"Starting binder: {startupBinder.GetType().Name}");
                startupBinder.Bind(services);
            }
        }

        public void Configure(IApplicationBuilder app, IEnumerable<IStartupService> startupServices) {
            foreach (var startupService in startupServices) {
                _logger.LogInformation($"Starting service: {startupService.GetType().Name}");
                startupService.Startup(app);
            }
        }

    }

Which our team found to be a very clean design. We had a chuckle at the response that logging is simply “not supported in this model”. Please reconsider whether this is the ideal way forward.

We certainly were not confused by the WebHost having a separate container. Maybe documentation was the answer instead of this backwards-incompatible change…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Generic Host restricts Startup constructor injection #353
One key behavior change here is that Host only uses one dependency injection container to build the application, as opposed to WebHost that...
Read more >
Avoiding Startup service injection in ASP.NET Core 3
The generic host does not allow injecting services into the Startup class. The first point has been pretty well publicised. Endpoint routing was ......
Read more >
c# - .Net Core Generic Host and Dependency Injection with ...
I ran into the same issue while trying to build a generic console app using the startup pattern that asp.net uses for web...
Read more >
Logging During Application Startup - Forty Years of Code
Back in April of 2019, Microsoft announced a change to ASP.NET Core's Startup class as a result of migrating the platform to the...
Read more >
Dependency injection in action
This guide explores many of the features of dependency injection (DI) in Angular. ... You can modify Angular's search behavior with the @Host...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found