'asp.net web api: action filter attribute is thread-safe by default?
Can some one confirm me that asp.net web api action filters are singleton/instance based by default? In asp.net core, I observed that it has one instance by default and the method is as per request-based. I am not sure on wweb.api action filters. Sorry if this question sounds silly.
using System.Web.Http.Filters;
namespace SampleApp.Attributes
{
public class CustomAttribute: Attribute, IActionFilter
{
public CustomAttribute()
{
// resolves dependencies here
}
public Task<HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext,
CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
{
// some code goes here with some update operations in sql server stored procedure level
}
}
}
Solution 1:[1]
As it states in the Microsoft documentation for filters (https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters?view=aspnetcore-6.0#thread-safety):
When passing an instance of a filter into Add, instead of its Type, the filter is a singleton and is not thread-safe.
If you're registering the filter type (rather than an instance), the framework will indeed create a new instance of the filter per request.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Marc Costello |