Skip to content

Generic Host restricts Startup constructor injection #14900

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

Closed
pranavkm opened this issue Oct 2, 2019 · 0 comments · Fixed by #15044
Closed

Generic Host restricts Startup constructor injection #14900

pranavkm opened this issue Oct 2, 2019 · 0 comments · Fixed by #15044
Assignees
Labels
breaking-change Indicates a .NET Core breaking change

Comments

@pranavkm
Copy link
Contributor

pranavkm commented Oct 2, 2019

Generic Host restricts Startup constructor injection

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.

Version introduced

3.0

Old behavior

The Startup constructor could constructor inject arbitrary types.

New behavior

The following types can be injected - IHostEnvironment, IWebHostEnvironment, and IConfiguration

Reason for change

This is a consequence of re-platting the web stack onto the generic host library.

Recommended action

You may mitigate this by inject services into Startup.Configure. For example
public void Configure(IApplicationBuilder app, IOptions<MyOptions> options)

Category

ASP.NET Core

Affected APIs

Application's Startup.


Issue metadata

  • Issue type: breaking-change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change Indicates a .NET Core breaking change
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants