zoukankan      html  css  js  c++  java
  • 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.

    原文链接

  • 相关阅读:
    select()函数用法二
    fcntl函数的用法总结
    O_NONBLOCK与O_NDELAY有何不同?
    struct termios结构体详解
    select()函数用法一
    linux下的struct sigaction
    sigaction 用法实例
    nand flash 的oob 及坏块管理
    wifi两种工作模式
    UBIFS文件系统简介 与 利用mkfs.ubifs和ubinize两个工具制作UBI镜像 (完整理解版本)
  • 原文地址:https://www.cnblogs.com/OpenCoder/p/12218501.html
Copyright © 2011-2022 走看看