ASP.NET Core 2.0 Dependency Injection

Here we will learn how to use ASP.NET Core service container for dependency injection.

Solution

Create a Service.

  1. public interface IGreetingService  
  2.     {  
  3.         string Greet(string to);  
  4.     }  
  5.    
  6.     public class GreetingService : IGreetingService  
  7.     {  
  8.         public string Greet(string to)  
  9.         {  
  10.             return $"Hello {to}";  
  11.         }  
  12.     } 

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

  1. public static class UseMiddlewareExtensions  
  2.     {  
  3.         public static IApplicationBuilder UseHelloWorld(this IApplicationBuilder app)  
  4.         {  
  5.             return app.UseMiddleware();  
  6.         }  
  7.     }  
  8.    
  9.     public class HelloWorldMiddleware  
  10.     {  
  11.         private readonly RequestDelegate next;  
  12.    
  13.         public HelloWorldMiddleware(  
  14.             RequestDelegate next)  
  15.         {  
  16.             this.next = next;  
  17.         }  
  18.    
  19.         public async Task Invoke(  
  20.             HttpContext context,  
  21.             IGreetingService greetingService)  
  22.         {  
  23.             var message = greetingService.Greet("World (via DI)");  
  24.             await context.Response.WriteAsync(message);  
  25.         }  
  26.     } 

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

  1. public void ConfigureServices(  
  2.             IServiceCollection services)  
  3.         {  
  4.             // setup dependency injection in service container  
  5.             services.AddScoped<IGreetingService, GreetingService>();  
  6.         }  
  7.    
  8.         public void Configure(  
  9.             IApplicationBuilder app,  
  10.             IHostingEnvironment env)  
  11.         {  
  12.             // setup request pipeline using middleware  
  13.             app.UseHelloWorld();  
  14.         }  

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.

  1. public class FlexibleGreetingService : IGreetingService  
  2.     {  
  3.         private readonly string sayWhat;  
  4.    
  5.         public FlexibleGreetingService(string sayWhat)  
  6.         {  
  7.             this.sayWhat = sayWhat;  
  8.         }  
  9.    
  10.         public string Greet(string to)  
  11.         {  
  12.             return $"{this.sayWhat} {to}";  
  13.         }  
  14.     }  

We can use factory method to add this to the service container.

  1. public void ConfigureServices(  
  2.            IServiceCollection services)  
  3.        {  
  4.            // setup dependency injection in service container  
  5.            services.AddScoped(factory =>  
  6.            {  
  7.                return new FlexibleGreetingService("Good Morning");  
  8.            });  
  9.        }  

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

  1. public void ConfigureServices(  
  2.      IServiceCollection services)  
  3.  {  
  4.      // setup dependency injection in service container  
  5.      services.AddSingleton(  
  6.          new FlexibleGreetingService("Good Evening"));  
  7.  }  

Discussion

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 the first time they are requested and stay the same for subsequent requests.

Note

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

Factory Methods

All the above methods 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 looks like: 

  1. AddScoped(Func<IServiceProvider, TService>)  

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, for example:

AddDbContextAddIdentityAddOptions and AddMvc.

Disposing Services

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

Request Services

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

  1. public class HelloDevelopersMiddleware  
  2. {  
  3.     private readonly RequestDelegate next;  
  4.   
  5.     public HelloDevelopersMiddleware(  
  6.         RequestDelegate next)  
  7.     {  
  8.         this.next = next;  
  9.     }  
  10.   
  11.     public async Task Invoke(  
  12.         HttpContext context)  
  13.     {  
  14.         var greetingService =   
  15.                context.RequestServices.GetService<IGreetingService>();  
  16.         var message = greetingService.Greet("Developers (via GetService)");  
  17.         await context.Response.WriteAsync(message);  
  18.     }  
  19. }  

Note

Add a "using" statement for Microsoft.Extensions.DependencyInjection to access the generic version of GetService() method.

Source Code

GitHub

Up Next
    Ebook Download
    View all
    Learn
    View all