ASP.NET Core 2.0 Dependency Injection


Use ASP.NET Core service container for dependency injection.


Create a service:

Inject where required, I am using the Middleware created in a previous post:

Add service to container using AddScoped() in ConfigureServices() method of Startup.cs:

Let’s say your service implementation needs more complex setup, you could use the overload that accepts a factory method. Let’s say our service accepts a parameter:

We can use factory method to add this to service container:

In case of Singleton lifetime, an additional overload accepts an instance of service:


ASP.NET Core comes with a built-in lightweight service container. We configure our services in ConfigureServices() method of Startup.cs. As discussed in a previous post, this method runs before Configure() method, hence we can use our services when setting up any middleware (including MVC).

The default method of injection is via public constructors, which for most scenarios is considered best practice.

Service Lifetimes

Service container manages the lifetime of services added to it. Following are the methods to accomplish this:

  • AddScoped(): These services are created once per request.
  • AddTransient(): These services are created each time they are requested.
  • AddSingleton(): These services are created first time they are requested and stays the same for subsequence requests.

Note: EF should be added as Scoped or using IServiceCollection.AddDbContext, which does this behind the scenes.

Factory Methods

Above methods all have an overload to add service using a factory method. This could be useful for services that require more complex setup, for instance using Builder pattern.

Signature for these methods look like:

Framework Services

IServiceCollection being received by ConfigureServices() has various built-in services (provided by framework), refer to ASP.NET Core documentation.

There are also some useful extension methods on IServiceCollection to add commonly used services e.g. AddDbContext, AddIdentity, AddOptions and AddMvc.

Disposing Services

Service container will call Dispose() on all types implementing IDisposable, except for services added as instance rather than type.

Request Services

Although injecting services via constructor is considered best practice, you could also retrieve the services using GetService() method on IServiceProvider, which can be accessed via HttpContext:

Note: add using statement for Microsoft.Extensions.DependencyInjection to access the generic version of GetService() method.

Source Code


Leave a Reply