ASP.NET Core MVC中的Filter如果不实现IFilterFactory接口,那么Filter默认情况下在ASP.NET Core生命周期内是单例的,会被重用
问
I have an AuthorizationFilter as follows:
public class AuthorizationFilterAttribute : Attribute, IAuthorizationFilter { public AuthorizationFilterAttribute() { //Constructor of AuthorizationFilter will be called one time } public void OnAuthorization(AuthorizationFilterContext context) { //OnAuthorization method will be called per http request } }
I found the constructor of AuthorizationFilter will just be called one time during the whole ASP.NET Core application lifetime. But its OnAuthorization method will be called per HTTP request.
Does it mean all filters (including IAuthorizationFilter,IActionFilter,IResourceFilter,IExceptionFilter etc) in ASP.NET Core MVC are singletons, which means they will be created just one time during ASP.NET Core application lifetime?
答1
It depends on the IFilterFactory.IsReusable property that is associated with your filter.
When the IFilterProvider (which by default is DefaultFilterProvider) is about to provide the desired instance, it first checks whether the filter implements IFilterFactory as well:
- If it does, it uses the filter's own IsReusable property to determine the lifetime of the instance.
- If not, it assumes the filter is reusable and IsReusable is set to true.
In the case of your custom AuthorizationFilterAttribute, since you don't implement IFilterProvider, it's indeed considered as reusable and will be created only once.
See Source
答2
I found an implementation that can create a filter instance per http request rather than ASP.NET Core application lifetime.
Actually, we need to create the filter internally and wrap it into an IFilterFactory like below:
using Microsoft.AspNetCore.Mvc.Filters; using System; namespace AspNetCoreFilterDemo.Filters { public class AuthorizationFilterWithFactoryAttribute : Attribute, IFilterFactory { //Return false, IFilterFactory.CreateInstance method will be called per http request //Return true, InternalAuthorizationFilter will still be singleton, since IFilterFactory.CreateInstance will be called only one time during the whole ASP.NET Core application lifetime public bool IsReusable { get { return false; } } private class InternalAuthorizationFilter : IAuthorizationFilter { public InternalAuthorizationFilter() { //This InternalAuthorizationFilter constructor will be called per http request rather than ASP.NET Core application lifetime } public void OnAuthorization(AuthorizationFilterContext context) { //OnAuthorization method will be called per http request } } public IFilterMetadata CreateInstance(IServiceProvider serviceProvider) { //Create InternalAuthorizationFilter instance per http request return new InternalAuthorizationFilter(); } } }
Please be aware of IFilterFactory.IsReusable property, we need to return false, otherwise the IFilterFactory.CreateInstance method will be called only one time during ASP.NET Core application lifetime, and InternalAuthorizationFilter is still singleton.
Then, we need to specify the AuthorizationFilterWithFactoryAttribute on controller instead of InternalAuthorizationFilter, but AuthorizationFilterWithFactoryAttribute will actually create and operate on an InternalAuthorizationFilter instance per http request:
using Microsoft.AspNetCore.Mvc; using AspNetCoreFilterDemo.Filters; namespace AspNetCoreFilterDemo.Controllers { public class HomeController : Controller { public HomeController() { } [AuthorizationFilterWithFactory] public IActionResult Index() { return View(); } } }
AuthorizationFilterWithFactoryAttribute will still be singleton and created one time, but we approached the target to create the filter (InternalAuthorizationFilter) per http request.
You can also take a reference from MSDN.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架