Inject Services (DI) into Filters
Today in this article we shall see how to Inject Services into Filters with and without Dependency injection.
Filters are good means of managing cross-cutting concerns.
Filters abstract your repetitive and important cross-cutting concerns to commonplace and make your business logic free of addressing those concerns implicitly.
Today in this article, we will cover below aspects,
Getting Started
Create ASP.NET Core API
Let’s create an ASP.NET core API application.
Please choose any ASP.NET Core API project template. The filter is supported in most ASP.NET Core project templates.
I have below service IUniqueIdService defined as scoped service instances.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddScoped<IUniqueIdService, UniqueIdService>();
}
In this instance, we will be trying to Dependency Inject (DI) UniqueIdService into Filters.
Here we will use particularly Action Filters as a filter for the DI.
Resolving other Services?
If you have any other services apart, you can very much follow any of the below two approaches discussed.
Approach 1 – Using IFilterFactory to resolve service instances
We shall use IFilterFactory implemented on an attribute using TypeFilterAttribute and IActionFilter.
We shall DI IUniqueIdService using constructor injection to create and resolve the scope of the instance using Action Filter.
Filters implementation IFilterFactory
are useful means when constructor DI is required for any dependencies.
Below is a sample implementation for the same with results showing the resolution of instances via DI.
Sample example
Above we are injecting the IUniqueIdService using Constructor injection
Below is how the Controller method is annotated with the Attribute [CustomSwag]
[HttpGet]
[CustomSwag]
public IEnumerable<WeatherForecast> GetUSAWeather()
{
var rng = new Random();
return GetWeatherForCast(rng);
}
The above Controller method is annotated with the attribute ‘CustomSwag‘. This attribute will let run the Filter logic before and after the actual business logic of Controller methods.
Approach 2 – Using TypeFilter to resolve service instances
This approach is simpler if you need DI services in Filters like action filters etc.
To use this option you need to perform below additional steps.
Step 1– Please annotate the controller method using ServiceFilter for the attribute,
[HttpGet]
[ServiceFilter(typeof(CustomAttribute))]
public IEnumerable<WeatherForecast> GetUSAWeather()
{
var rng = new Random();
return GetWeatherForCast(rng);
}
Step2 – ServiceFilter must be registered in ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddScoped<IUniqueIdService, UniqueIdService>();
services.AddScoped<CustomAttribute>();
}
Approach 3 – Using HttpContext.RequestServices to resolve service instance
We shall DI IUniqueIdService using HTTP Context object to create and resolve instances using Action Filter. as below,
Below is a sample example,
The above code lets you create an instance of IUniqueIdService and is able to work with its method easily.
The below Controller method is annotated with the attribute ‘SwagVersion‘. This attribute will let run the required Filter logic before and after actual business logic.
[HttpGet]
[SwagVersion]
public IEnumerable<WeatherForecast> GetUSAWeather()
{
var rng = new Random();
return GetWeatherForCast(rng);
}
References:
Either way, as discussed above you shall be able to resolve the services from the Filters easily.
Do you have any comments or ideas or any better suggestions to share?
Please sound off your comments below.
Happy Coding !!
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.