How does ASP.NET 5 inject startup's dependencies?
I'm studying ASP.NET 5 documentation (It's great and better than the old one.) I understand that ASP.NET 5 includes a simple built-in inversion of control (IoC) container that supports constructor injection by default. As far as I know, configuring services and dependencies are done inside ConfigureServices()
method.
The ConfigureServices()
method is called after StartUp
method.
So my question is : how does ASP.NET 5 internally inject Startup's dependencies?
I'd like to know that because if I want to inject another dependency, for example IFooEnviroment
how can I do that?
The logic for this lives in the Hosting layer of ASP.NET 5:
Startup
type is determined IServiceCollection
You can of course register your own services in ConfigureServices
. But they won't be available in the constructor as you already figured. There is no way to add your own services to the runtime services. This makes sense because there should be a difference between runtime services and application services.
You can however inject services registered in the ConfigureServices()
method in the Configure()
method.