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的定義吧:

 public virtual bool InvokeAction(ControllerContext controllerContext, string actionName) {
            if (controllerContext == null) {
                throw new ArgumentNullException("controllerContext");
            }
            if (String.IsNullOrEmpty(actionName)) {
                throw new ArgumentException(MvcResources.Common_NullOrEmpty, "actionName");
            }

            ControllerDescriptor controllerDescriptor = GetControllerDescriptor(controllerContext);
            ActionDescriptor actionDescriptor = FindAction(controllerContext, controllerDescriptor, actionName);
            if (actionDescriptor != null) {
                FilterInfo filterInfo = GetFilters(controllerContext, actionDescriptor);

                try {
                    AuthorizationContext authContext = InvokeAuthorizationFilters(controllerContext, filterInfo.AuthorizationFilters, actionDescriptor);
                    if (authContext.Result != null) {
                        // the auth filter signaled that we should let it short-circuit the request
                        InvokeActionResult(controllerContext, authContext.Result);
                    }
                    else {
                        if (controllerContext.Controller.ValidateRequest) {
                            ValidateRequest(controllerContext);
                        }

                        IDictionary<string, object> parameters = GetParameterValues(controllerContext, actionDescriptor);
                        ActionExecutedContext postActionContext = InvokeActionMethodWithFilters(controllerContext, filterInfo.ActionFilters, actionDescriptor, parameters);
                        InvokeActionResultWithFilters(controllerContext, filterInfo.ResultFilters, postActionContext.Result);
                    }
                }
                catch (ThreadAbortException) {
                    // This type of exception occurs as a result of Response.Redirect(), but we special-case so that
                    // the filters don't see this as an error.
                    throw;
                }
                catch (Exception ex) {
                    // something blew up, so execute the exception filters
                    ExceptionContext exceptionContext = InvokeExceptionFilters(controllerContext, filterInfo.ExceptionFilters, ex);
                    if (!exceptionContext.ExceptionHandled) {
                        throw;
                    }
                    InvokeActionResult(controllerContext, exceptionContext.Result);
                }

                return true;
            }

            // notify controller that no method matched
            return false;
        }

  這個方法里面的內容不可能一次講完的,我們看看  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。

  public MethodInfo FindActionMethod(ControllerContext controllerContext, string actionName) {
            List<MethodInfo> methodsMatchingName = GetMatchingAliasedMethods(controllerContext, actionName);
            methodsMatchingName.AddRange(NonAliasedMethods[actionName]);
            List<MethodInfo> finalMethods = RunSelectionFilters(controllerContext, methodsMatchingName);

            switch (finalMethods.Count) {
                case 0:
                    return null;

                case 1:
                    return finalMethods[0];

                default:
                    throw CreateAmbiguousMatchException(finalMethods, actionName);
            }
        }

  

循環每個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特性還可以在類上面


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM