Access Configuration apsettings.json in Filters
Today in this article we shall see how to access Configuration apsettings.json in filters and other resources in the Filters using the C# application in ASP.NET Core.
Today in this article, we will cover below aspects,
Filters are good means of managing cross-cutting concerns.
Filters abstract your repetitive and important cross-cutting concern to commonplace and make your business logic free of addressing those concerns implicitly.
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 shall be using below sample apsettings.json to demonstarte this,
apsettings.json
{
"AllowedHosts": "*",
"CustomerConfig": {
"CustomerKeyurl": "https://www.thecodebuzz.com/key",
"CustomerdetailsUrl": "https://www.thecodebuzz.com/id",
"Agency": {
"AgencyID": "subvalue1_from_json",
"AccountKey": 200
}
}
}
I have below service ConfigureServices defined with nothing specific done to access the IConfiguration.
This is due to IConfiguration is available by default via DI in any part of the module.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddScoped<IUniqueIdService, UniqueIdService>();
}
If you have typesafe access or IOption implementation of IConfiguration access then the below registration will be different a bit.
Please visit the article below for more information,
In this instance, we will be trying to Dependency Inject (DI) IConfiguration into Filters.
Here we will use particularly Action Filters as a filter then try to access it in the filters
Approach 1 – Using IFilterFactory to resolve instances
We shall use IFilterFactory implemented on an attribute using TypeFilterAttribute and IActionFilter.
We shall DI IConfiguration using constructor injection to create and resolve the scope of the instance using Action Filter.
Filters implementing IFilterFactory
are useful means when constructor DI is required for any dependencies.
Below is a sample implementation for the same with results showing resolution of instances via DI.
Sample example,
Above we are injecting the IConfiguration using Constructor injection.
If you have configuration using IOption then you can use the same steps to get access to configuration values access in the Filters.
Please learn more on Typesafe configuration access in ASP.NET Core
Below how the Controller method is annotated with the [CustomSwag] Attribute,
[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 IConfiguration instances
This approach is simpler if you need DI IConfiguration in Filters like action filters etc.
Getting IConfiguration is simple and can be accessed in a regular way i.e DI using constructor injection.
To use this option you need to perform below additional steps for configuring Filter attribute.
Step 1– Please annotate 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>();
}
References:
Either way, as discussed above you shall be able to resolve the IConfiguration from the Filters easily.
That’s all! Happy coding!
Does this help you fix your issue?
Do you have any better solutions or suggestions? Please sound off your comments below.
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.