The IServiceCollection interface IServiceCollection used to build a dependency injection container. After a complete build, it is assembled into an instance of IServiceProvider that can be used to resolve services. You can embed IServiceProvider in any class. IApplicationBuilder and HttpContext can provide a service provider through ApplicationServices or RequestServices respectively.
IServiceProvider defines GetService(Type type) to enable the service:
var service = (IFooService)serviceProvider.GetService(typeof(IFooService));
There are also several convenient extension methods, such as serviceProvider.GetService<IFooService>() (add using for Microsoft.Extensions.DependencyInjection ).
Enabling Services Inside the Launch Class
Dependency Injection
The runtime can embed services in the constructor of the Startup class, such as IHostingEnvironment , IConfiguration and IServiceProvider . Please note that this service provider is an instance created at the hosting level and contains only services for launching the application.
Services can also be added to the Configure() method. You can add an arbitrary parameter list after the IApplicationBuilder parameter. You can also add your own services that are registered in the ConfigureServices() method; here they will be decided not by the hosting service provider, but by the application service provider.
public void Configure(IApplicationBuilder app, IFooService fooService) {
However, the ConfigureServices() method does not allow services to be deployed; it accepts the IServiceCollection argument of IServiceCollection . This is the method in which you configure your application dependency injection container. You can use the services added to the startup constructor here. For example:
public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) {
Manual dependency resolution
If you want to enable services manually, you can IServiceProvider let the runtime implement the IServiceProvider instance in the constructor or use the ApplicationServices provided by IApplicationBuilder in IApplicationBuilder Configure() :
public Startup(IServiceProvider serviceProvider) { var hostingEnv = serviceProvider.GetService<IHostingEnvironment>(); }
or
public void Configure(IApplicationBuilder app) { var serviceProvider = app.ApplicationServices; var hostingEnv = serviceProvider.GetService<IHostingEnvironment>(); }
However, if you need to enable services in the ConfigureServices() method, you need a different approach. You can create an intermediate IServiceProvider from an IServiceProvider instance that contains the services that are registered so far:
public void ConfigureServices(IServiceCollection services) { services.AddSingleton<IFooService, FooService>();
To do this, you need the Microsoft.Extensions.DependencyInjection package.
Please pay attention:
As a rule, you should not allow services inside the ConfigureServices() method, since this is where you configure application services. Sometimes you just need access to some IOptions<MyOptions> . You can do this, IConfiguration values ββfrom an IConfiguration instance with an MyOptions instance (which, in essence, does the parameter infrastructure):
public void ConfigureServices(IServiceCollection services) { var myOptions = new MyOptions(); Configuration.GetSection("SomeSection").Bind(myOptions); }
Manually authorizing services (aka Service Locator) is usually called an anti-pattern . Although it has its own use cases (for frameworks and / or infrastructural levels), you should avoid it as much as possible.