How is ASP. NET Web API HttpController activated?

Source: Internet
Author: User

HttpController and HttpControllerDescriptor
Assembly Parsing
HttpController type resolution
Select the target HttpController type
Create the target HttpController

Through the above content, we have a deep understanding of the core objects of the HttpController activation system. These objects include AssembliesResolver and HttpControllerTypeResolver for parsing the Assembly and valid HttpController types, httpControllerSelector selected for the complete target HttpController is used to activate HttpControllerActivator of the target HttpController instance and DependencyResolver as the IoC container.

The UML shown in the right figure reveals the interfaces and types corresponding to these core objects and their relationships. After the received requests flow through the message pipeline and are processed accordingly, the above objects collaborate to complete the activation of the target HttpConrtoller, then ASP. what is the specific HttpController activation process for NET Web APIs?

For Message Processing pipelines that constitute the core framework of ASP. NET Web APIs, The HttpMessageHandler at the end is an HttpRoutingDispatcher object. After it completes route resolution (HttpRoutingDispatcher's route resolution only takes place in Self Host mode. For the Web Host mode, routing resolution is implemented by ASP. by default, it will pass the request to an HttpControllerDispatcher object.

HttpControllerDispatcher activates and executes the target HttpController object, and returns the HttpResponseMessage object representing the execution result to the HttpRoutingDispatcher object, the latter sends the HttpResponseMessage back to the Message Queue for corresponding processing, and finally completes the response to the request.

The UML shown in the left figure shows the entire process of activating HttpController. After the HttpControllerDispatcher takes over the request, it obtains the registered HttpControllerSelector object and calls its SelectController method to obtain the HttpControllerDescriptor object describing the target HttpController.

By default, the registered HttpControllerSelector is a DefaultHttpControllerSelector object. The latter obtains all HttpController types by means of the registered HttpControllerTypeResolver object, and creates a ing between the HttpControllerDescriptor object and the HttpController name. After its SelectController method is executed, it only needs to obtain the name of the target HttpController Based on the HttpRouteData object carried by the request, and select the corresponding HttpControllerDescriptor from the ing relationship.

HttpControllerDispatcher then calls the CreateController method of the HttpControllerDescriptor object to obtain the activated HttpController object. For this HttpControllerDescriptor object, when its CreateController method is called, it will get the registered HttpControllerActivator object, call the Create method to activate the target HttpController object and return the activated object.

For the default registered DefaultHttpControllerActor object, it uses the registered DependencyResolver to obtain the object representing the target HttpController instance based on the HttpController type. If the latter returns an HttpController object, the object will be taken directly as the return value of the method. Otherwise, DefaultHttpControllerActor will create the target HttpController object in the form of reflection and return it.

The default registered DependencyResolver is an EmptyResolver object, and the returned HttpController object is always Null. Therefore, the activated HttpController object is always created in the form of reflection by default. Because of this, the defined HttpController type must have a default constructor.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.