asp.net mvc源码分析-Controllerl篇 ControllerDescriptor

在上篇asp.net mvc源码分析-Controllerl篇 TempData数据存储 我们讲到了ActionInvoker.InvokeAction(ControllerContext, actionName) 这句,当时跳过了,现在我们首先来看看ActionInvoker属性的定义吧:

  public IActionInvoker ActionInvoker {
            get {
                if (_actionInvoker == null) {
                    _actionInvoker = CreateActionInvoker();
                }
                return _actionInvoker;
            }
            set {
                _actionInvoker = value;
            }
        }
 protected virtual IActionInvoker CreateActionInvoker() {
            return new ControllerActionInvoker();
        }

和TempDataProvider属性定义一样,大家一定要习惯这些代码啊。

而ControllerActionInvoker的定义也很简单,但是这个类却不简单啊。

让我们来看看你InvokeAction的定义吧:

  这个方法里面的内容不可能一次讲完的,我们看看  ControllerDescriptor controllerDescriptor = GetControllerDescriptor(controllerContext); 很明显ControllerDescriptor是Controller实例的一个包装类。     protected virtual ControllerDescriptor GetControllerDescriptor(ControllerContext controllerContext) {
            Type controllerType = controllerContext.Controller.GetType();
            ControllerDescriptor controllerDescriptor = DescriptorCache.GetDescriptor(controllerType, () => new ReflectedControllerDescriptor(controllerType));
            return controllerDescriptor;
        }

从这个方法中,我们可以知道实际返回的是一个ReflectedControllerDescriptor实例,它是ControllerDescriptor的子类,DescriptorCache.GetDescriptor(...)好像是从缓存中获取的啊,让我们证实一下吧,先来看看ControllerDescriptorCache的GetDescriptor方法:

 internal sealed class ControllerDescriptorCache : ReaderWriterCache<Type, ControllerDescriptor> {
        public ControllerDescriptor GetDescriptor(Type controllerType, Func<ControllerDescriptor> creator) {
            return FetchOrCreateItem(controllerType, creator);
        }
    }

FetchOrCreateItem方法很简单,从缓存中获取ControllerDescriptor ,如果没有就创建并加入缓存然后在返回,缓存实现方式其实就是一个字典Dictionary<TKey, TValue>。

现在看看ReflectedControllerDescriptor的够着函数是否有什么特别之处:

  _controllerType = controllerType;
            _selector = new ActionMethodSelector(_controllerType);

怎么又有ActionMethodSelector这个东东啊,其构造函数如下      

  public ActionMethodSelector(Type controllerType) {
            ControllerType = controllerType;
            PopulateLookupTables();
        }

      private void PopulateLookupTables() {
            MethodInfo[] allMethods = ControllerType.GetMethods(BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public);
            MethodInfo[] actionMethods = Array.FindAll(allMethods, IsValidActionMethod);
            AliasedMethods = Array.FindAll(actionMethods, IsMethodDecoratedWithAliasingAttribute);
            NonAliasedMethods = actionMethods.Except(AliasedMethods).ToLookup(method => method.Name, StringComparer.OrdinalIgnoreCase);
        }

这个方法很简单,找出ControllerType的所有实例、共有方法,然后在过滤调不是Action的,最后吧这些Action方法分成两部分,一部分有别名,一部分没有别名。

现在我们已经得到了ControllerDescriptor实例,下面应该来看看ActionDescriptor actionDescriptor = FindAction(controllerContext, controllerDescriptor, actionName);这句代码了;同样我们可以确认ActionDescriptor实际上一个Action的包装类。

  protected virtual ActionDescriptor FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, string actionName)这个方法实际上就是调用

ControllerDescriptor类FindAction方法,让我们看看你ReflectedControllerDescriptor的FindAction方法,该方法很简单,组要就2句代码:

   MethodInfo matched = _selector.FindActionMethod(controllerContext, actionName);
return new ReflectedActionDescriptor(matched, actionName, this);

_selector.FindActionMethod(controllerContext, actionName); 这句就是找到我们需要Action对应的MethodInfo。

  

循环每个MethodInfo,查找它们的自定义的ActionMethodSelectorAttribute特性,如果有只返回验证通过的特性。看到ReflectedAttributeCache.GetActionMethodSelectorAttributes(methodInfo)这样的代码感觉由于缓存有关,
  private static ReadOnlyCollection<TAttribute> GetAttributes<TMemberInfo, TAttribute>(ConcurrentDictionary<TMemberInfo, ReadOnlyCollection<TAttribute>> lookup, TMemberInfo memberInfo)
            where TAttribute : Attribute
            where TMemberInfo : MemberInfo {
            return lookup.GetOrAdd(memberInfo, mi => new ReadOnlyCollection<TAttribute>((TAttribute[])memberInfo.GetCustomAttributes(typeof(TAttribute), inherit: true)));
        }

ReflectedAttributeCache这个类有几个缓存字典:

ConcurrentDictionary<MethodInfo, ReadOnlyCollection<ActionMethodSelectorAttribute>>

ConcurrentDictionary<MethodInfo, ReadOnlyCollection<ActionNameSelectorAttribute>> 

ConcurrentDictionary<MethodInfo, ReadOnlyCollection<FilterAttribute>>

ConcurrentDictionary<Type, ReadOnlyCollection<FilterAttribute>>

默认实现ActionMethodSelectorAttribute类主要有以下几个

AcceptVerbsAttribute
HttpDeleteAttribute
HttpGetAttribute
HttpPostAttribute
HttpPutAttribute
NonActionAttribute

AcceptVerbsAttribute

剩下的就是直接实例一个ReflectedActionDescriptor对象了,这个也没什么特殊,只是里面有一个验证方法

if (validateMethod) {
                string failedMessage = VerifyActionMethodIsCallable(methodInfo);
                if (failedMessage != null) {
                    throw new ArgumentException(failedMessage, "methodInfo");
                }
            }

用来验证该方法是否可以执行,以下几种情况经不会通过,(1)方法是静态方法(2)方法的实例类型不是ControllerBase(3)是否包含泛型参数如 public ActionResult Index<T>()是非法的,但 public ActionResult Index(List<string> aa)是合法(4)参数中不能含有Ref和out。

   这篇文章说的很散,我们需要注意一点微软在mvc里面缓存做的很好了,在前面个将获取ControllerTyper时它是有缓存的,一次读取当前程序集所有的ControllerType,在这里提到了一个DescriptorCache 缓存每次调用的ControllerType->ReflectedControllerDescriptor,而ReflectedControllerDescriptor实例会一次去读该Controller的所有Action方法;这里还有一个ReflectedAttributeCache,缓存每次调用MethodInfo的所有特性(ActionMethodSelectorAttribute、ActionNameSelectorAttribute、FilterAttribute),当然FilterAttribute特性还可以在类上面

posted on   dz45693  阅读(2208)  评论(5编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构

导航

< 2012年11月 >
28 29 30 31 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示