It's great that ASP.NET DI works out-of-the-box and recursively resolves all constructor dependencies. Though sometimes you want to be able to access DI container directly. I am wondering if there is a way? Maybe something like this:
IService service = Container.Instance.Resolve<IService>();
I didn't find anything in the docs (though I know, I can replace built-in DI framework).
For the most part you don't need it, but there are some specific cases. In my situation I need to initialize my EF DbContext using IoC container on app start-up to initiate auto-migrations that supposed to work like that with EF Core:
using (var context = new MyContext())
{
context.Database.Migrate();
}
My context is registered in container but it's in a separate class library. And I don't want to call it's DbContextOptions constructor directly.
After the question got updated and it's clear that it's going about EntityFramework Core migrations, there is an semi-official way form the ASP.NET Core team on how to correctly resolve the
DbContextduring application startup.By default, the
DbContextis registered as scoped service, so you get instantiated once per request. The catch during the application startup is, that there is no context yet and onlyapp.ApplicationServicesis available withinConfiguremethod and theApplicationServicesprovider is essentially resolving singletons (scoped is a singleton per scope and application scope lives as long as the application does).So the trick is to create a scope first, resolve the
DbContext, do the operations and then dispose the context (and theDbContextwith it).Examples can be found in the MusicStore example application here and here.
As of right now, this is the only safe way to resolve it w/o causing any issues with i.e. disposed object exceptions.
Also please note, that the earliest point you can do this is in the
Configuremethod, because only then the IoC container has been built. InConfigureServicesyou only populate theIServiceCollection.The relevant code snippets:
Edit:
Additional resources regarding this issue: