[Dot Net Core](Graphic series )9. Comparison of Dot Net Core MVC and Asp.NET MVC-Timing of Controller Factory and Provider Decision

Czxdas
3 min readJan 6, 2022

In the section “Http Request to Dot Net Core MVC”, there is an overview when Http Request enters .Net Core MVC, it will use ActionInvokerFactory to further analyze the follow-up work of Controller and Action.

At this time, I think, what is the difference between Dot Net Core MVC Controller Factory and Asp.NET Web MVC when determining the timing of Controller Factory Class formation?

Recall that when EndpointMiddleware is first executed, it will first obtain the service of IActionInvokerFactory through the Provider provided by Dot NET Core’s own IOC mechanism, that is, ServiceProviderEngineScope.

As shown above, IActionInvokerFactory will Resolve into ActionInvokerFactory entity. At this time, the constructor will continue to be resolved into entities through DI. The relationship diagram is as follows:

Before ControllerFactoryProvider is resolved, it will first resolve its sub-parameter IControllerFactory type, which is DefaultControllerFactory.

When EndpointMiddleware.Invoke is executed for the first time, the process (refer to Http Request to Dot Net Core MVC) will execute ActionInvokerFactory.CreateInvoker, and then execute ControllerActionInvokerCache.GetCachedResult. At this time, because ControllerActionInvokerCacheEntry has not been generated instance, it will generate this The instance first prepares the controllerFactory delegation function. The “CreateController” delegate function is prepared by ControllerFactoryProvider.CreateControllerFactory and passed back to the controllerFactory. This delegation function is responsible for ControllerActivatorProvider.CreateActivator and provides a program for ServiceProviderEngineScope to resolve the Controller class instance.

Finally, the controllerFactory that can generate Controller instance is finally assigned to ControllerActionInvokerCacheEntry.ControllerFactory. And how to implement this ControllerFactory can refer to “Resolve Controller class process”.

Next, let’s take a look at when ASP.NET Web MVC determines the generation of Factory:

ASP.NET Web MVC

The above figure shows ASP.NET Web MVC, the red box is the key time point when the Controller entity is generated. MvcHandler will get the Factory that generates the Controller. The factory’s previous initialization will construct the DependencyResolver, and the Controller type will be generated according to the Type, and the System.Reflection component will be used further.

The main difference between the two MVCs is that Dot Net Core generates Controller Factory around the IOC mechanism, while ASP.NET Web MVC uses the parent category of MvcHandler to find DefaultDependencyResolver to generate Controller Factory.

--

--