• 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.

    原文链接

  • 相关阅读:
    MYSQL一对多,两表查询合并数据
    bootstrap瀑布流代码
    os mac apache+php+mysql环境配置
    centos 6.5 服务器安装 (LNMP ntfs文件支持 PHP-RPM CHROOT沙盒)
    在linux下将当前目录文件全部小写含目录名
    Javascript知识汇总------js中容易被忽略的细节(持续更新)
    Javascript知识汇总------js数据类型隐式转换
    下次要写约瑟夫和并查集
    --wrong answer
    --最小生成树
  • 原文地址:https://www.cnblogs.com/OpenCoder/p/12218501.html
Copyright © 2020-2023  润新知