ASP.NET MVC 請求處理涉及五大組件的同步/非同步執行方式,本文總結前三個:MvcHandler、Controller、ActionInvoker。
同時實作 IHttpHandler 與 IHttpAsyncHandler,永以非同步方式處理請求(呼叫 BeginProcessRequest/EndProcessRequest)。
實作 IController (同步) 與 IAsyncController (非同步)。預設 Controller 類別同時實作兩介面,且 DisableAsyncSupport 預設為 false,故 預設以非同步執行。若覆寫該屬性為 true,則改為同步執行。
protected virtual IAsyncResult BeginExecute(RequestContext requestContext, AsyncCallback callback, object state)
{
if (this.DisableAsyncSupport) { /* 同步執行 */ }
else { /* 非同步執行 */ }
}
ControllerActionInvoker:同步執行 ActionAsyncControllerActionInvoker:非同步執行 ActionController 預設建立 ControllerActionInvoker,但可透過覆寫 CreateActionInvoker 替換為非同步版本。