上一節概述了由Http Request 到 Dot Net Core MVC 的流程,本節會探討Controller 於 Dot Net Core 被Resolve的大致流程。 首先延續上篇文章,ControllerActionInvoker.InvokeAsync 會執行controller instance 的產生:
上圖展示ControllerActionInvoker 會收集 ControllerContext、ControllerActionInvokerCacheEntry 的資訊到自己的屬性中,一旦開始執行ControllerActionInvoker.InvokeAsync,會由S1步驟開始,呼叫ControllerActionInvoker.InvokeFilterPipelineAsync,經過S2呼叫Next,到了S3步驟呼叫ControllerActionInvoker.InvokeInnerFilterAsync,此時就會到一個很重要的階段,就是透過 ControllerFactory 去接續產生 Controller instance。
程式碼:
var controller = controllerActivator(controllerContext);
首先會執行ActivatorUtilities執行 GetService 函式。S1步驟會先由ControllerContext.HttpContext.RequestServices 取得 ServiceProviderEngine.CreateScope,執行此函式可以得到ServiceProviderScope。
接著S2步驟透過由ControllerContext取得Controller Type來當參數執行ServiceProviderScope.GetService,至S3步驟到ServiceProviderEngine.GetService,會呼叫 ServiceProvider的 IServiceProviderEngineCallbackOnResolve。
下圖是將整個呼叫的過程LOG下來,當作軌跡:
由上圖執行ControllerActionInvoker.InvokeAsync,會從 Authorization Filter Case,接著 Resource Filter Case、Exception Filter Case、Action Case,到了ActionBegin 開始找到 ControllerFactory 產生 controller instance,再到 ActionFilter Case 來執行 Action Method,最後回傳 ActionResult。 以上是大致上的過程概述,提供參考。
When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.
評論