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

來源:互聯網
上載者:User

在上篇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特性還可以在類上面。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.