Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure DI services available to the Startup class constructor

When I create the webhost for an ASP.NET Core application I can specify the Startup class but not an instance.

The constructor of your own Startup class can take parameter which are provided through DI. I know how to register services for DI within ConfigureServices but as that is a member of that class these services are not available for the constructor of my startup class.

How do I register services which will be available as constructor parameter of the Startup class?

The reason is that I have to supply an object instance which must be created outside/before the webhost is created and I do not want to pass it in a global-like style.

Code to create the IWebHost:

this.host = new WebHostBuilder()
    .UseConfiguration(config)
    .UseKestrel()
    .UseIISIntegration()
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseStartup<WebStartup>()
log.Debug("Run webhost");
this.host.Start();

Constructor of WebStartup:

public WebStartup(IHostingEnvironment env, MyClass myClass)
{
    var config = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddEnvironmentVariables()
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
        .Build();
    ...
}

So specifically, how to I register MyClass in this example (which obviously must be done before WebStartup is instanciated by the IWebHost)?

like image 520
ZoolWay Avatar asked Oct 24 '16 15:10

ZoolWay


People also ask

What is the use of the Configure method of the startup class?

The Configure method is used to specify how the app responds to HTTP requests. The request pipeline is configured by adding middleware components to an IApplicationBuilder instance. IApplicationBuilder is available to the Configure method, but it isn't registered in the service container.

How do I add dependency injection to startup CS?

The implementation class of above interface having dummy data. Then we have to create a Controller(API) for calling this repo, and inject this interface into this. In the last, we have to register it in Startup class. Also mention which type of instance want to inject - (the lifetime) of our instance.

Which method of startup class can be used to inject the service into request pipeline?

The Configure method is a place where you can configure application request pipeline for your application using IApplicationBuilder instance that is provided by the built-in IoC container. ASP.NET Core introduced the middleware components to define a request pipeline, which will be executed on every request.


1 Answers

Although Steven's concerns are valid and you should take note of them, it is technically possible to configure the DI container that is used to resolve your Startup class.

ASP.NET hosting uses dependency injection to wire up an instance of your Startup class and also let us add our own services to that container using the ConfigureServices extension method on IWebHostBuilder:

var host = new WebHostBuilder()
    .UseKestrel()
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseIISIntegration()
    .ConfigureServices(services => services.AddSingleton<IMyService, MyService>())
    .UseStartup<Startup>()
    .Build();

host.Run();

and:

public Startup(IMyService myService)
{
    myService.Test();
}

In fact, all that UseStartup<WebStartup> does is adding it as a service implementation of IStartup to the hosting DI container (see this).

Please note that instances of your services will be resolved again in the application container as a new instance of the IServiceProvider will be built. The registration of the services will, however, be passed to the application IServiceCollection in your Startup class.

like image 54
Henk Mollema Avatar answered Sep 30 '22 06:09

Henk Mollema