This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
DI doesn't work as expected in Development builds #6407
Comments
In 2.0.0 the DI container is configured to help you not shoot yourself in the foot (but only in development because of fear of breaking people's running albeit probably buggy apps). Scoped dependencies shouldn't be injected directly into the Configure method, instead, you should create a scope and resolve your dependency from that scope: public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<ScopedThing>();
}
public void Configure(IApplicationBuilder app)
{
using (var scope = app.ApplicationServices.CreateScope())
{
var scopedThing = scope.ServiceProvider.GetRequiredService<ScopedThing>();
}
}
} |
I want to create it the default scope (e.g. the request scope), isn't this just doing the wrong thing? or is it just because I'm doing work in the Configure method that I need a scope (in Configure) since I am not in a request scope yet? |
I get it now. Thanks @davidfowl! |
FWIW aspnet/Hosting#1106 |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I have a service I want to register in Startup.ConfigureServices then inject into Startup.Configure but it fails in Development builds but not release builds. I'm using 2.0.0-preview-1 builds. I have a repro case attached. To reproduce:
The exception is as follows in the development execution:
TestDI.zip
The text was updated successfully, but these errors were encountered: