前文简介:我们抽象类路由规则的对象,RouteBase是路由对象的抽象基类,ASP.NET 的路由系统中有唯一一个从RouteBase继承的路由对象,那就是Route类型了。我们注册了路由对象Route,UrlRoutingModule截获请求,把当前请求的Url地址和RouteTable路由表中注册的路由对象一个一个的比较,如果没有找到就返回Null,请求就到此终止了。如果有匹配的路由对象,选择第一个匹配的Route对象,并且根据该Route对象,生成了路由数据RouteData对象,本对象是为了封装成RequestContext对象,RequestContext对象封装了RouteData和HttpContext对象。
其实RequestContext对象也是作为参数使用的,我们根据Route对象获得了RouteData,该对象有一个RouteHandler属性,这个属性的值在ASP.NET MVC中是MvcRouteHandler。RouteHandler主要的作用是提供用于处理最终请求的HttpHandler对象,代码如下:
IHttpHandler=RouteData.RouteHandler.GetHttpHandler(requestContext);HttpContext.Remap(IHttpHandler);
上文中我们获取到的RequestContext,将此作为参数调用RouteHandler的GetHttpHandler方法,我们获得了IHttpHandler对象,我们需要把请求交给这个HttpHandler来处理,通过HttpContext.Remap(Handler)实现请求的交接,这个Handler在ASP.NET MVC中就是MvcHandler,我们既然获得了HttpHandler对象,也实现了请求的交接,下一步该做什么呢?
一、概览
我们获得了用于处理请求HttpHandler对象,下一步最重要的任务是把Controller对象找到,RouteData对象的Values属性的Controller键和Action键的值只是一个字符串的结果,我们要通过这个名称找到具体的Controller类型实例,才能执行Action方法,给客户想要的应答。这篇文章就是Controller的激活方法的详解。
ASP.NET MVC系统真的很庞大,也很复杂,如果我们一上来就瀑布式的说,就像流水账一样,估计也不太容易说明白。为了便于理解,我们人为的把ASP.NET MVC这么庞大的系统拆分成相互独立,又有一定联系的多个子系统,然后我们各个击破,理解起来也就简单了,最后在把各个部分整合在一起,理解就全面了。今天我们要说的这一部分暂时叫做Controller激活系统吧,激活系统有两个含义,一是我们要找到我们需要的Controller对象,并实例化;二是我们药缓存他,并要执行它。
我们先来看看MvcHandler的源码吧,有助于我们的理解,代码如下:
1 ///Selects the controller that will handle an HTTP request. 2 public class MvcHandler : IHttpAsyncHandler, IHttpHandler, IRequiresSessionState 3 { 4 private struct ProcessRequestState 5 { 6 internal IAsyncController AsyncController; 7 8 internal IControllerFactory Factory; 9 10 internal RequestContext RequestContext; 11 12 internal void ReleaseController() 13 { 14 this.Factory.ReleaseController(this.AsyncController); 15 } 16 } 17 18 private static readonly object _processRequestTag = new object(); 19 20 internal static readonly string MvcVersion = MvcHandler.GetMvcVersionString(); 21 22 ///Contains the header name of the ASP.NET MVC version. 23 public static readonly string MvcVersionHeaderName = "X-AspNetMvc-Version"; 24 25 private ControllerBuilder _controllerBuilder; 26 27 internal ControllerBuilder ControllerBuilder 28 { 29 get 30 { 31 if (this._controllerBuilder == null) 32 { 33 this._controllerBuilder = ControllerBuilder.Current; 34 } 35 return this._controllerBuilder; 36 } 37 set 38 { 39 this._controllerBuilder = value; 40 } 41 } 42 43 ///Gets or sets a value that indicates whether the MVC response header is disabled. 44 ///true if the MVC response header is disabled; otherwise, false. 45 public static bool DisableMvcResponseHeader 46 { 47 get; 48 set; 49 } 50 51 ///Gets a value that indicates whether another request can use the 52 ///instance. true if the 53 protected virtual bool IsReusable 54 { 55 get 56 { 57 return false; 58 } 59 } 60 61 ///instance is reusable; otherwise, false. Gets the request context. 62 ///The request context. 63 public RequestContext RequestContext 64 { 65 get; 66 private set; 67 } 68 69 ///Gets a value that indicates whether another request can use the 70 ///instance. true if the 71 bool IHttpHandler.IsReusable 72 { 73 get 74 { 75 return this.IsReusable; 76 } 77 } 78 79 ///instance is reusable; otherwise, false. Initializes a new instance of the 80 /// The request context. 81 ///class. The 82 public MvcHandler(RequestContext requestContext) 83 { 84 if (requestContext == null) 85 { 86 throw new ArgumentNullException("requestContext"); 87 } 88 this.RequestContext = requestContext; 89 } 90 91 ///parameter is null. Adds the version header by using the specified HTTP context. 92 /// The HTTP context. 93 protected internal virtual void AddVersionHeader(HttpContextBase httpContext) 94 { 95 if (!MvcHandler.DisableMvcResponseHeader) 96 { 97 httpContext.Response.AppendHeader(MvcHandler.MvcVersionHeaderName, MvcHandler.MvcVersion); 98 } 99 }100 101 ///Called by ASP.NET to begin asynchronous request processing. 102 ///The status of the asynchronous call. 103 /// The HTTP context.104 /// The asynchronous callback method.105 /// The state of the asynchronous object.106 protected virtual IAsyncResult BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, object state)107 {108 HttpContextBase httpContext2 = new HttpContextWrapper(httpContext);109 return this.BeginProcessRequest(httpContext2, callback, state);110 }111 112 ///Called by ASP.NET to begin asynchronous request processing using the base HTTP context. 113 ///The status of the asynchronous call. 114 /// The HTTP context.115 /// The asynchronous callback method.116 /// The state of the asynchronous object.117 protected internal virtual IAsyncResult BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, object state)118 {119 IController controller;120 IControllerFactory factory;121 this.ProcessRequestInit(httpContext, out controller, out factory);122 IAsyncController asyncController = controller as IAsyncController;123 if (asyncController != null)124 {125 BeginInvokeDelegatebeginDelegate = delegate(AsyncCallback asyncCallback, object asyncState, MvcHandler.ProcessRequestState innerState)126 {127 IAsyncResult result;128 try129 {130 result = innerState.AsyncController.BeginExecute(innerState.RequestContext, asyncCallback, asyncState);131 }132 catch133 {134 innerState.ReleaseController();135 throw;136 }137 return result;138 };139 EndInvokeVoidDelegate endDelegate = delegate(IAsyncResult asyncResult, MvcHandler.ProcessRequestState innerState)140 {141 try142 {143 innerState.AsyncController.EndExecute(asyncResult);144 }145 finally146 {147 innerState.ReleaseController();148 }149 };150 MvcHandler.ProcessRequestState invokeState = new MvcHandler.ProcessRequestState151 {152 AsyncController = asyncController,153 Factory = factory,154 RequestContext = this.RequestContext155 };156 SynchronizationContext synchronizationContext = SynchronizationContextUtil.GetSynchronizationContext();157 return AsyncResultWrapper.Begin (callback, state, beginDelegate, endDelegate, invokeState, MvcHandler._processRequestTag, -1, synchronizationContext);158 }159 Action action = delegate160 {161 try162 {163 controller.Execute(this.RequestContext);164 }165 finally166 {167 factory.ReleaseController(controller);168 }169 };170 return AsyncResultWrapper.BeginSynchronous(callback, state, action, MvcHandler._processRequestTag);171 }172 173 /// Called by ASP.NET when asynchronous request processing has ended. 174 /// The asynchronous result.175 protected internal virtual void EndProcessRequest(IAsyncResult asyncResult)176 {177 AsyncResultWrapper.End(asyncResult, MvcHandler._processRequestTag);178 }179 180 private static string GetMvcVersionString()181 {182 return new AssemblyName(typeof(MvcHandler).Assembly.FullName).Version.ToString(2);183 }184 185 ///Processes the request by using the specified HTTP request context. 186 /// The HTTP context.187 protected virtual void ProcessRequest(HttpContext httpContext)188 {189 HttpContextBase httpContext2 = new HttpContextWrapper(httpContext);190 this.ProcessRequest(httpContext2);191 }192 193 ///Processes the request by using the specified base HTTP request context. 194 /// The HTTP context.195 protected internal virtual void ProcessRequest(HttpContextBase httpContext)196 {197 IController controller;198 IControllerFactory controllerFactory;199 this.ProcessRequestInit(httpContext, out controller, out controllerFactory);200 try201 {202 controller.Execute(this.RequestContext);203 }204 finally205 {206 controllerFactory.ReleaseController(controller);207 }208 }209 210 private void ProcessRequestInit(HttpContextBase httpContext, out IController controller, out IControllerFactory factory)211 {212 HttpContext current = HttpContext.Current;213 if (current != null && ValidationUtility.IsValidationEnabled(current) == true)214 {215 ValidationUtility.EnableDynamicValidation(current);216 }217 this.AddVersionHeader(httpContext);218 this.RemoveOptionalRoutingParameters();219 string requiredString = this.RequestContext.RouteData.GetRequiredString("controller");220 factory = this.ControllerBuilder.GetControllerFactory();221 controller = factory.CreateController(this.RequestContext, requiredString);222 if (controller == null)223 {224 throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, MvcResources.ControllerBuilder_FactoryReturnedNull, new object[]225 {226 factory.GetType(),227 requiredString228 }));229 }230 }231 232 private void RemoveOptionalRoutingParameters()233 {234 RouteValueDictionary values = this.RequestContext.RouteData.Values;235 values.RemoveFromDictionary((KeyValuePairentry) => entry.Value == UrlParameter.Optional);236 }237 238 /// Enables processing of HTTP Web requests by a custom HTTP handler that implements the 239 /// Aninterface. object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) that are used to service HTTP requests.240 void IHttpHandler.ProcessRequest(HttpContext httpContext)241 {242 this.ProcessRequest(httpContext);243 }244 245 /// Called by ASP.NET to begin asynchronous request processing using the base HTTP context. 246 ///The status of the asynchronous call. 247 /// The HTTP context.248 /// The asynchronous callback method.249 /// The data.250 IAsyncResult IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)251 {252 return this.BeginProcessRequest(context, cb, extraData);253 }254 255 ///Called by ASP.NET when asynchronous request processing has ended. 256 /// The asynchronous result.257 void IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)258 {259 this.EndProcessRequest(result);260 }261 }
MvcHandler里面这个两个方法定义了Controller激活系统实现的骨架:
1 ///Processes the request by using the specified base HTTP request context. 2 /// The HTTP context. 3 protected internal virtual void ProcessRequest(HttpContextBase httpContext) 4 { 5 IController controller; 6 IControllerFactory controllerFactory; 7 this.ProcessRequestInit(httpContext, out controller, out controllerFactory); 8 try 9 {10 controller.Execute(this.RequestContext);11 }12 finally13 {14 controllerFactory.ReleaseController(controller);15 }16 }17 18 private void ProcessRequestInit(HttpContextBase httpContext, out IController controller, out IControllerFactory factory)19 {20 HttpContext current = HttpContext.Current;21 if (current != null && ValidationUtility.IsValidationEnabled(current) == true)22 {23 ValidationUtility.EnableDynamicValidation(current);24 }25 this.AddVersionHeader(httpContext);26 this.RemoveOptionalRoutingParameters();27 string requiredString = this.RequestContext.RouteData.GetRequiredString("controller");28 factory = this.ControllerBuilder.GetControllerFactory();29 controller = factory.CreateController(this.RequestContext, requiredString);30 if (controller == null)31 {32 throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, MvcResources.ControllerBuilder_FactoryReturnedNull, new object[]33 {34 factory.GetType(),35 requiredString36 }));37 }38 }
代码不是很难,大家一看就能看懂,下一节好好的介绍一些Controller激活系统中所涉及到的各个对象,具体的解析规则要到下一篇文章了,否则文章就太长了。
二、Controller对象激活详述
ASP.NET MVC中,我们人为分解的当前系统叫:Controller激活系统,其实这个激活系统里面包含的对象也不是很多,主要有我们要实例化的Controller对象,管理Controller的ControllerFactory对象,管理ControllerFactory的ControllerBuilder对象,这三个主要对象构成了我们的激活系统。
1、我们先看看Controller类型的定义吧
我们所说的Controller其实是指实现了IController接口的某个类型实例。Controller是一个可以执行的对象,它的执行体现在对Execute方法上的调用。一说到执行我们是不是就会想到同步执行和异步执行呢,既然Controller是一个可以执行的对象,他会不会也具有同步和异步的执行呢,答案是肯定的,我们先来看看两个接口吧:
public interface IController{ ///Executes the specified request context. /// The request context. void Execute(RequestContext requestContext);}
这个就是同步版的接口,该接口的命名空间是:System.Web.Mvc,当目标Controller对象被成功激活后,对请求的后续处理和最终响应都通过执行者Execute方法来实现。我们再看看异步版的接口定义吧:
1 public interface IAsyncController : IController 2 { 3 ///Executes the specified request context. 4 ///The status of the asynchronous operation. 5 /// The request context. 6 /// The asynchronous callback method. 7 /// The state. 8 IAsyncResult BeginExecute(RequestContext requestContext, AsyncCallback callback, object state); 9 10 ///Ends the asynchronous operation. 11 /// The asynchronous result.12 void EndExecute(IAsyncResult asyncResult);13 }
异步版接口定义的命名空间是:System.Web.Mvc.Async,我们看到IAsyncController是实现了IController的接口,Controller的异步执行是通过调用BeginExecute和EndExecute方法来实现的。我们通过Visual Studio创建的ASP.NET MVC项目,我们自定义的Controller实现的基类是Controller类型,不是直接实现以上接口的。我们看看Controller类型的定义吧:
public abstract class Controller : ControllerBase, IActionFilter, IAuthenticationFilter, IAuthorizationFilter, IDisposable, IExceptionFilter, IResultFilter, IAsyncController, IController, IAsyncManagerContainer{//代码省略}
Controller是抽象类型,实现了很多接口,因为该类的代码太多,所以省略了部分内部代码,这个里面牵扯到另外一个类,就是ControllerBase,
1 ///Represents the base class for all MVC controllers. 2 public abstract class ControllerBase : IController 3 { 4 private readonly SingleEntryGate _executeWasCalledGate = new SingleEntryGate(); 5 6 private DynamicViewDataDictionary _dynamicViewDataDictionary; 7 8 private TempDataDictionary _tempDataDictionary; 9 10 private bool _validateRequest = true; 11 12 private IValueProvider _valueProvider; 13 14 private ViewDataDictionary _viewDataDictionary; 15 16 ///Gets or sets the controller context. 17 ///The controller context. 18 public ControllerContext ControllerContext 19 { 20 get; 21 set; 22 } 23 24 ///Gets or sets the dictionary for temporary data. 25 ///The dictionary for temporary data. 26 public TempDataDictionary TempData 27 { 28 get 29 { 30 if (this.ControllerContext != null && this.ControllerContext.IsChildAction) 31 { 32 return this.ControllerContext.ParentActionViewContext.TempData; 33 } 34 if (this._tempDataDictionary == null) 35 { 36 this._tempDataDictionary = new TempDataDictionary(); 37 } 38 return this._tempDataDictionary; 39 } 40 set 41 { 42 this._tempDataDictionary = value; 43 } 44 } 45 46 ///Gets or sets a value that indicates whether request validation is enabled for this request. 47 ///true if request validation is enabled for this request; otherwise, false. The default is true. 48 public bool ValidateRequest 49 { 50 get 51 { 52 return this._validateRequest; 53 } 54 set 55 { 56 this._validateRequest = value; 57 } 58 } 59 60 ///Gets or sets the value provider for the controller. 61 ///The value provider for the controller. 62 public IValueProvider ValueProvider 63 { 64 get 65 { 66 if (this._valueProvider == null) 67 { 68 this._valueProvider = ValueProviderFactories.Factories.GetValueProvider(this.ControllerContext); 69 } 70 return this._valueProvider; 71 } 72 set 73 { 74 this._valueProvider = value; 75 } 76 } 77 78 ///Gets the dynamic view data dictionary. 79 ///The dynamic view data dictionary. 80 [Dynamic] 81 public dynamic ViewBag 82 { 83 [return: Dynamic] 84 get 85 { 86 if (this._dynamicViewDataDictionary == null) 87 { 88 this._dynamicViewDataDictionary = new DynamicViewDataDictionary(() => this.ViewData); 89 } 90 return this._dynamicViewDataDictionary; 91 } 92 } 93 94 ///Gets or sets the dictionary for view data. 95 ///The dictionary for the view data. 96 public ViewDataDictionary ViewData 97 { 98 get 99 {100 if (this._viewDataDictionary == null)101 {102 this._viewDataDictionary = new ViewDtaDictionary();103 }104 return this._viewDataDictionary;105 }106 set107 {108 this._viewDataDictionary = value;109 }110 }111 ///Executes the specified request context. 112 /// The request context.113 ///The 114 protected virtual void Execute(RequestContext requestContext)115 {116 if (requestContext == null)117 {118 throw new ArgumentNullException("requestContext");119 }120 if (requestContext.HttpContext == null)121 {122 throw new ArgumentException(MvcResources.ControllerBase_CannotExecuteWithNullHttpContext, "requestContext");123 }124 this.VerifyExecuteCalledOnce();125 this.Initialize(requestContext);126 using (ScopeStorage.CreateTransientScope())127 {128 this.ExecuteCore();129 }130 }131 132 ///parameter is null. Executes the request. 133 protected abstract void ExecuteCore();134 135 ///Initializes the specified request context. 136 /// The request context.137 protected virtual void Initialize(RequestContext requestContext)138 {139 this.ControllerContext = new ControllerContext(requestContext, this);140 }141 142 internal void VerifyExecuteCalledOnce()143 {144 if (!this._executeWasCalledGate.TryEnter())145 {146 string message = string.Format(CultureInfo.CurrentCulture, MvcResources.ControllerBase_CannotHandleMultipleRequests, new object[]147 {148 base.GetType()149 });150 throw new InvalidOperationException(message);151 }152 }153 154 ///Executes the specified request context. 155 /// The request context.156 void IController.Execute(RequestContext requestContext)157 {158 this.Execute(requestContext);159 }160 }
ControllerBase实现了IController接口,但是没有实现IAsyncController接口,说明ControllerBase是一个同步的基类,之所以单独又增加ControllerBase,个人认为它定义了方法调用的扩展点。ControllerBase是一个抽象类,通过显示的实现了IController的Execute方法,在这个显示实现的方法IController.Execute方法里面又调用了受保护的虚方法Execute,这个受保护的虚方法Execute又在内部调用了抽象方法ExecuteCore,作为ControllerBase的继承者,必须通过实现对抽象方法ExecuteCore来完成对Controller的执行。这个过程就是方法调用的扩展点,而且ControllerBase也抽象所有Controller中都会用到的一些共同的属性,如TempData,ControllerContext,ValueProvider,ViewBag,ViewData等,大家可以细细研究,这里就不细说了,都很简单。
在ControllerBase里面有一个初始化的方法,这个方法的作用是根据当前Controller对象和当前请求上下文RequestContext创建ControllerContext对象,在请求后续处理的时候很多地方会用到ControllerContext。
///Initializes the specified request context. /// The request context.protected virtual void Initialize(RequestContext requestContext){ this.ControllerContext = new ControllerContext(requestContext, this);}
此方法是虚方法,我们可以扩展他,这个方法的调用点在受保护的虚方法Exuecute中,在调用ExecuteCore之前调用了该方法。
我们说了接口IController,IAsyncController,也说了抽象类ControllerBase,我们最后对我们要经常使用的基类做一下简述,我们真正在项目中使用的Controller的基类型是Controller类型,Controller实现了IController接口和IAsyncController接口,说明我们自己的Controller是可以同步执行,也可以异步执行的,同时实现了IDisposable接口,说明Controller类型是需要释放的,谁来释放激活的Controller的类型呢,那就是我们接下来要说的一个对象ControllerFactory,这里不多说,下面会解释清楚的。
最后我们需要说明的,虽然Controller可以同步执行,也可以异步执行,但是是同步还是异步是靠DisableAsyncSupport属性控制的,返回的是布尔值,默认情况是false,意思是指支持异步执行,以下代码说明实际情况。当BeginExecute方法执行的时候,它会根据该属性的值决定调用Execute同步执行Controller,还是调用BeginExecuteCore/EndExecuteCore方法异步执行Controller,换句话说,如果总是希望Controller同步执行,那就要把DisableAsyncSupport属性设置为true。
1 protected virtual IAsyncResult BeginExecute(RequestContext requestContext, AsyncCallback callback, object state) 2 { 3 if (this.DisableAsyncSupport) 4 { 5 Action action = delegate 6 { 7 this.Execute(requestContext); 8 }; 9 return AsyncResultWrapper.BeginSynchronous(callback, state, action, Controller._executeTag);10 }11 if (requestContext == null)12 {13 throw new ArgumentNullException("requestContext");14 }15 16 this.Initialize(requestContext);17 BeginInvokeDelegatebeginDelegate = (AsyncCallback asyncCallback, object callbackState, Controller controller) => controller.BeginExecuteCore(asyncCallback, callbackState);18 EndInvokeVoidDelegate endDelegate = delegate(IAsyncResult asyncResult, Controller controller)19 {20 controller.EndExecuteCore(asyncResult);21 };22 return AsyncResultWrapper.Begin (callback, state, beginDelegate, endDelegate, this, Controller._executeTag, -1, null);23 }
2、ControllerContext
在ASP.NET MVC中我们会遇到很多上下文(Context)的类型,比如:RequestContext,它是对HttpContext和RouteData的封装,今天我们看看ControllerContext,就是指Controller的上下文,再说明白一点就是Controller在执行过程中对需要用到的一些数据的封装对象,它类似一个Facade,里面包含了一些在Controller执行时所要用的一些对象,必须表示请求的HttpContextBase对象。这个类型没什么可说的,很简单,我把反编译的代码也都贴出来了,大家可以自己看。
1 ///Encapsulates information about an HTTP request that matches specified 2 public class ControllerContext 3 { 4 private sealed class EmptyHttpContext : HttpContextBase 5 { 6 } 7 8 internal const string ParentActionViewContextToken = "ParentActionViewContext"; 9 10 private HttpContextBase _httpContext; 11 12 private RequestContext _requestContext; 13 14 private RouteData _routeData; 15 16 ///and instances. Gets or sets the controller. 17 ///The controller. 18 public virtual ControllerBase Controller 19 { 20 get; 21 set; 22 } 23 24 ///Gets the display mode. 25 ///The display mode. 26 public IDisplayMode DisplayMode 27 { 28 get 29 { 30 return DisplayModeProvider.GetDisplayMode(this.HttpContext); 31 } 32 set 33 { 34 DisplayModeProvider.SetDisplayMode(this.HttpContext, value); 35 } 36 } 37 38 ///Gets or sets the HTTP context. 39 ///The HTTP context. 40 public virtual HttpContextBase HttpContext 41 { 42 get 43 { 44 if (this._httpContext == null) 45 { 46 this._httpContext = ((this._requestContext != null) ? this._requestContext.HttpContext : new ControllerContext.EmptyHttpContext()); 47 } 48 return this._httpContext; 49 } 50 set 51 { 52 this._httpContext = value; 53 } 54 } 55 56 ///Gets a value that indicates whether the associated action method is a child action. 57 ///true if the associated action method is a child action; otherwise, false. 58 public virtual bool IsChildAction 59 { 60 get 61 { 62 RouteData routeData = this.RouteData; 63 return routeData != null && routeData.DataTokens.ContainsKey("ParentActionViewContext"); 64 } 65 } 66 67 ///Gets an object that contains the view context information for the parent action method. 68 ///An object that contains the view context information for the parent action method. 69 public ViewContext ParentActionViewContext 70 { 71 get 72 { 73 return this.RouteData.DataTokens["ParentActionViewContext"] as ViewContext; 74 } 75 } 76 77 ///Gets or sets the request context. 78 ///The request context. 79 public RequestContext RequestContext 80 { 81 get 82 { 83 if (this._requestContext == null) 84 { 85 HttpContextBase httpContext = this.HttpContext ?? new ControllerContext.EmptyHttpContext(); 86 RouteData routeData = this.RouteData ?? new RouteData(); 87 this._requestContext = new RequestContext(httpContext, routeData); 88 } 89 return this._requestContext; 90 } 91 set 92 { 93 this._requestContext = value; 94 } 95 } 96 97 ///Gets or sets the URL route data. 98 ///The URL route data. 99 public virtual RouteData RouteData100 {101 get102 {103 if (this._routeData == null)104 {105 this._routeData = ((this._requestContext != null) ? this._requestContext.RouteData : new RouteData());106 }107 return this._routeData;108 }109 set110 {111 this._routeData = value;112 }113 }114 115 ///Initializes a new instance of the 116 public ControllerContext()117 {118 }119 120 ///class. Initializes a new instance of the 121 /// The controller context.122 ///class by using the specified controller context. The 123 protected ControllerContext(ControllerContext controllerContext)124 {125 if (controllerContext == null)126 {127 throw new ArgumentNullException("controllerContext");128 }129 this.Controller = controllerContext.Controller;130 this.RequestContext = controllerContext.RequestContext;131 }132 133 ///parameter is null. Initializes a new instance of the 134 /// The HTTP context.135 /// The route data.136 /// The controller.137 public ControllerContext(HttpContextBase httpContext, RouteData routeData, ControllerBase controller) : this(new RequestContext(httpContext, routeData), controller)138 {139 }140 141 ///class by using the specified HTTP context, URL route data, and controller. Initializes a new instance of the 142 /// The request context.143 /// The controller.144 ///class by using the specified request context and controller. One or both parameters are null. 145 public ControllerContext(RequestContext requestContext, ControllerBase controller)146 {147 if (requestContext == null)148 {149 throw new ArgumentNullException("requestContext");150 }151 if (controller == null)152 {153 throw new ArgumentNullException("controller");154 }155 this.RequestContext = requestContext;156 this.Controller = controller;157 }158 }
3、ControllerFactory
我们的Controller对象有了,为了解耦和可扩展性,ASP.NET MVC 框架中不可能直接new一个Controller的实例对象吧,我们在MvcHandler的代码中也看到了是基于接口编程的,所以需要提供一种机制,来提供我们要得到的Controller对象。既然不能直接new一个Controller出来,那我们就封装new的过程,用工厂来代替吧。对了,就是工厂,这个工厂就是ControllerFactory,从名字上可以看出它的意思,创建Controller对象的工厂。ASP.NET MVC是可以扩展的,可以自定义的,我们的ControllerFactory工厂也不例外,所以我们就产生了ControllerFactory工厂的接口,接口的名字是IControllerFactory,所有的ControllerFactory都必须实现该接口。接口定义如下:
1 ///Defines the methods that are required for a controller factory. 2 public interface IControllerFactory 3 { 4 ///Creates the specified controller by using the specified request context. 5 ///The controller. 6 /// The request context. 7 /// The name of the controller. 8 IController CreateController(RequestContext requestContext, string controllerName); 9 10 ///Gets the controller's session behavior. 11 ///The controller's session behavior. 12 /// The request context.13 /// The name of the controller whose session behavior you want to get.14 SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName);15 16 ///Releases the specified controller. 17 /// The controller.18 void ReleaseController(IController controller);19 }
IControllerFactory接口是对管理Controller对象的工厂的抽象,怎么管理呢,无非就是我要创建一个Controller就能创建一个Controller。我们创建好了Controller,不使用的情况就要能销毁它,能创建和能销毁就构成了IControllerFactory的主要功能,CreateController用于创建Controller对象实例,ReleaseController方法用于销毁不用的Controller对象,因为我们的Controller实现了IDisposable接口。想起来了吗?因为我们自定义的Controller都继承了抽象基类Controller类型,该抽象Controller基类实现了IDisposable接口。
IControllerFactory接口还有一个方法GetControllerSessionBehavior,用于管理Controller的会话状态,返回的类型为SessionStateBehavior枚举值,它有四个枚举值:Default(使用默认的ASP.NET逻辑来确定请求的会话行为),Required(为请求启用完全的读和写会话状态的行为),ReadOnly(为请求启用只读的会话状态),Disabled(禁用会话状态)。说起来话长,在System.Web.SessionState命名空间下有两个接口定义,分别是IRequiresSessionState(实现本接口,HttpHandler采用Required会话模式)和IReadOnlySessionState(实现本接口,HttpHandler采用ReadOnly会话模式)类型,这个两个接口是标记接口,没有定义具体的方法。我们在看看我们的MvcHandler类型的定义吧,代码如下:
public class MvcHandler : IHttpAsyncHandler, IHttpHandler, IRequiresSessionState
具体采用何种会话状态行为取决于当前Http请求上下文(HttpContext的静态属性Current表示的对象),对于ASP.NET 3.0及其以前的版本,我们不能对当前的Http上下文的会话状态行为模式进行修改,在ASP.NET 4.0中为HttpContext对象定义了一个SetSessionStateBehavior方法,此方法也在HttpContextBase定义了,HttpContextBase的子类HttpContextWrapper重写了这个方法,在内部调用HttpContext的同名方法来设置当前请求的会话状态模式。
4、ControllerBuilder
我们有了Controller,可以通过IController,IAsyncController,ControllerBase或者Controller抽象基类来自定义我们自己的Controller基类型,我们也有了IControllerFactory接口,可以管理Controller对象,也可以定义我们自己的ControllerFactory来取代系统的默认实现。问题来了,我们自定义了ControllerFactory对象,但是如何把我们自定义的ControllerFactory放到ASP.NET MVC框架中呢,那就需要我们这个类型了,它的名字就是ControllerBuilder。
代码最有说服力,我们先上代码吧,源码如下:
1 ///Represents a class that is responsible for dynamically building a controller. 2 public class ControllerBuilder 3 { 4 private static ControllerBuilder _instance = new ControllerBuilder(); 5 6 private Func_factoryThunk = () => null; 7 8 private HashSet _namespaces = new HashSet (StringComparer.OrdinalIgnoreCase); 9 10 private IResolver _serviceResolver; 11 12 /// Gets the current controller builder object. 13 ///The current controller builder. 14 public static ControllerBuilder Current 15 { 16 get 17 { 18 return ControllerBuilder._instance; 19 } 20 } 21 22 ///Gets the default namespaces. 23 ///The default namespaces. 24 public HashSetDefaultNamespaces 25 { 26 get 27 { 28 return this._namespaces; 29 } 30 } 31 32 /// Initializes a new instance of the 33 public ControllerBuilder() : this(null) 34 { 35 } 36 37 internal ControllerBuilder(IResolverclass. serviceResolver) 38 { 39 IResolver arg_6A_1 = serviceResolver; 40 if (serviceResolver == null) 41 { 42 arg_6A_1 = new SingleServiceResolver (() => this._factoryThunk(), new DefaultControllerFactory 43 { 44 ControllerBuilder = this 45 }, "ControllerBuilder.GetControllerFactory"); 46 } 47 this._serviceResolver = arg_6A_1; 48 } 49 50 /// Gets the associated controller factory. 51 ///The controller factory. 52 public IControllerFactory GetControllerFactory() 53 { 54 return this._serviceResolver.Current; 55 } 56 57 ///Sets the specified controller factory. 58 /// The controller factory. 59 ///The 60 public void SetControllerFactory(IControllerFactory controllerFactory) 61 { 62 if (controllerFactory == null) 63 { 64 throw new ArgumentNullException("controllerFactory"); 65 } 66 this._factoryThunk = (() => controllerFactory); 67 } 68 69 ///parameter is null. Sets the controller factory by using the specified type. 70 /// The type of the controller factory. 71 ///The 72 ///parameter is null. The controller factory cannot be assigned from the type in the 73 ///parameter. An error occurred while the controller factory was being set. 74 public void SetControllerFactory(Type controllerFactoryType) 75 { 76 if (controllerFactoryType == null) 77 { 78 throw new ArgumentNullException("controllerFactoryType"); 79 } 80 if (!typeof(IControllerFactory).IsAssignableFrom(controllerFactoryType)) 81 { 82 throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, MvcResources.ControllerBuilder_MissingIControllerFactory, new object[] 83 { 84 controllerFactoryType 85 }), "controllerFactoryType"); 86 } 87 this._factoryThunk = delegate 88 { 89 IControllerFactory result; 90 try 91 { 92 result = (IControllerFactory)Activator.CreateInstance(controllerFactoryType); 93 } 94 catch (Exception innerException) 95 { 96 throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, MvcResources.ControllerBuilder_ErrorCreatingControllerFactory, new object[] 97 { 98 controllerFactoryType 99 }), innerException);100 }101 return result;102 };103 }104 }
源码不是很多,我贴出了完整的代码。
ControllerBuilder定义了一个静态只读属性Current,返回当前使用的ControllerBuilder对象。该类型有一个返回类型为IControllerFactory的方法GetControllerFactory,这个方法用于获取当前注册的ControllerFactory对象。ControllerBuilder类型还有两个重载的SetControllerFactory方法,这两个方法的主要作用是把我们自定义的ControllerFactory注册到ASP.NET MVC框架中去。但是这两个方法有些不同,参数类型为IControllerFactory的SetControllerFactory方法直接注册ControllerFactory实例对象,而参数类型为Type的SetControllerFactory方法注册的ControllerFactory的类型。
如果我们注册的是ControllerFactory类型的话,那么GetControllerFactory方法在获取ControllerFactory对象的时候都要通过反射来获得,也就是说针对GetControllerFactory方法的每次调用都会伴随着ControllerFactory的实例化,ASP.NET MVC框架不会对实例化的ControllerFactory进行缓存。如果我们注册的是ControllerFactory对象实例,针对GetControllerFactory方法来说是直接返回注册的对象,就性能而言,注册ControllerFactory对象比较好。
命名空间
如果在多个命名空间下定义了多个同名的Controller类型,如果只是按着名称来匹配就会导致激活系统无法确定具体的Controller的类型而抛出异常。为了解决这个问题,我们必须为定义了同名的Controller类型的命名空间设置不同的优先级。
ASP.NET MVC的Controller的激活系统为我们提供了两种提升命名空间优先级的方法。第一种方法就是通过调用RouteCollectionExtensions的扩展方法MapRoute的时候提供命名空间列表。此种方式指定的命名空间列表会保存在Route路由对象的DataTokens属性中,对应的Key是Namespaces。第二种方式就是把命名空间列表添加到当前的ControllerBuilder类型的默认命名空间列表中。这些命名空间列表存放在返回类型为HashSet<string>的DefaultNamespaces属性中。这两种方法的优先级第一种方法更高。
对于Area的命名空间来说,如果我们在调用AreaRegistrationContext对象的MapRoute方法时提供了命名空间,该命名空间会作为Route对象的命名空间,如果没有提供,则AreaRegistration类型所在的命名空间,再加上“.*”作为Route对象的命名空间。当我们调用AreaRegistrationContext的MapRoute方法的时候,会在Route对象的DataTokens属性里增加一个Key为UseNamespaceFallback的变量,它表示是否采用后备的命名空间来解析Controller类型。如果Route对象有命名空间,该值就是False,否则就是true。
解析过程是,ASP.NET MVC会先使用RouteData包含的命名空间,如果解析失败,它会从RouteData对象的DataTokens属性中取出Key为UseNamespaceFallback的值,如果该值为true或者不存在,就使用ControllerBuidler的默认命名空列表来解析。如果UseNamespaceFallback的值为false,就不实用后备的命名空间,如果没找到就会抛出异常。
三、激活Controller类型的缓存和释放
一个比较大的系统里面,可能所涉及到的Controller类型成千上万,如果每次都要通过反射的方式及时获取,那对性能的影响是很客观的,ASP.NET MVC框架组想到了这一点,针对Controller和AreaRegistration都实现了彻底的缓存,这个缓存是把所有解析出来的类型持久化到物理文件中,两个物理文件的名称分别是:MVC-ControllerTypeCache.xml和MVC-AreaRegistrationTypeCache.xml,具体的目录在%windir%\Microsoft.Net\Framework\v{version}\Temporary ASP.NET Files\root\..\..\UserCache\。
当接收到web应用被启动后的第一个请求的时候,Controller激活系统会读取这些文件,通过反序列化生成List<Type>对象。
MVC-ControllerTypeCache.xml代码如下:
-- - MVCTest.Controllers.AccountController MVCTest.Controllers.HomeController MVCTest.Controllers.ManageController
Controller的释放很简单,直接上代码吧。
///Releases the specified controller. /// The controller to release.public virtual void ReleaseController(IController controller){ IDisposable disposable = controller as IDisposable; if (disposable != null) { disposable.Dispose(); }}
由于所有的Controller类型都实现了IDisposable接口,所以我们可以直接调用当前Controller对象的Dispose方法即可。
四、小结
好了,这篇文章就先写到这里吧,Controller的激活系统还没有完,今天只是从总体上来讲一下,内容很多,很难在一片文章里把所有东西都写到。这个概览先给大家一个整体上的感觉吧,下一篇文章就具体写一下Controller的激活解析过程。
写一写很不错,把自己的理解写出来,现在更清晰了,也希望高手来指正和赐教。