Problem
Use ASP.NET Core service container for dependency injection.
Solution
Create a service:
public interface IGreetingService
{
string Greet(string to);
}
public class GreetingService : IGreetingService
{
public string Greet(string to)
{
return $"Hello {to}";
}
}
Inject where required, I am using the Middleware
created in a previous post:
public static class UseMiddlewareExtensions
{
public static IApplicationBuilder UseHelloWorld(this IApplicationBuilder app)
{
return app.UseMiddleware();
}
}
public class HelloWorldMiddleware
{
private readonly RequestDelegate next;
public HelloWorldMiddleware(
RequestDelegate next)
{
this.next = next;
}
public async Task Invoke(
HttpContext context,
IGreetingService greetingService)
{
var message = greetingService.Greet("World (via DI)");
await context.Response.WriteAsync(message);
}
}
Add service to container using AddScoped()
in ConfigureServices()
method of Startup.cs:
public void ConfigureServices(
IServiceCollection services)
{
services.AddScoped<IGreetingService, GreetingService>();
}
public void Configure(
IApplicationBuilder app,
IHostingEnvironment env)
{
app.UseHelloWorld();
}
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:
public class FlexibleGreetingService : IGreetingService
{
private readonly string sayWhat;
public FlexibleGreetingService(string sayWhat)
{
this.sayWhat = sayWhat;
}
public string Greet(string to)
{
return $"{this.sayWhat} {to}";
}
}
We can use factory method to add this to service container:
public void ConfigureServices(
IServiceCollection services)
{
services.AddScoped(factory =>
{
return new FlexibleGreetingService("Good Morning");
});
}
In case of Singleton lifetime, an additional overload accepts an instance of service:
public void ConfigureServices(
IServiceCollection services)
{
services.AddSingleton(
new FlexibleGreetingService("Good Evening"));
}
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 first time they are requested and stay the same for subsequence requests.
Note: EF should be added as Scoped or using IServiceCollection.AddDbContext
, which does this behind the scenes.
Factory Methods
The 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:
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, 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
:
public class HelloDevelopersMiddleware
{
private readonly RequestDelegate next;
public HelloDevelopersMiddleware(
RequestDelegate next)
{
this.next = next;
}
public async Task Invoke(
HttpContext context)
{
var greetingService =
context.RequestServices.GetService<IGreetingService>();
var message = greetingService.Greet("Developers (via GetService)");
await context.Response.WriteAsync(message);
}
}
Note: Add using
statement for Microsoft.Extensions.DependencyInjection
to access the generic version of GetService()
method.