asp.net MVC involves 5 synchronous and asynchronous, are you stupid to know? [Last Article]

Source: Internet
Author: User
Tags abstract execution httpcontext

The execution of the action method has two basic forms, that is, synchronous execution and asynchronous execution, and in the entire system of ASP.NETMVC involves a lot of synchronous/asynchronous execution, although in the previous corresponding article has been introduced accordingly, in order to give the reader a holistic understanding of this, Let's make a concluding discussion.

Synchronization and Asynchrony of Mvchandler

For the asp.net MVC application, Mvchandler is the HttpHandler that is ultimately used to process the request, which is dynamically mapped to the corresponding request by UrlRoutingModule the HttpModule that implements the URL route. Mvchandler activates and executes the target controller with Controllerfactory and is responsible for releasing the activated controller after the execution is completed, please participate in Chapter 3rd "Controller Activation" in this book. As the following code fragment shows, Mvchandler implements both the IHttpHandler and IHttpAsyncHandler interfaces, so it always calls beginprocessrequest/ The EndProcessRequest method processes the request in an asynchronous manner.

   1:public class Mvchandler:ihttpasynchandler, IHttpHandler, ...
2: {
3: //other Members
4: IAsyncResult Ihttpasynchandler.beginprocessrequest (HttpContext context, AsyncCallback cb, Object Extradata);
5: void ihttpasynchandler.endprocessrequest (IAsyncResult result);
6: void Ihttphandler.processrequest (HttpContext HttpContext);
7:}

Synchronization and Asynchrony of two-controller

Controller also has two versions of synchronous and asynchronous, which implement two interfaces IController and Iasynccontroller with the following definitions. When the activated controller object is executed in the Mvchandler BeginProcessRequest method, if the controller type implements the Iasynccontroller interface, The Beginexecute/endexecute method is invoked to execute the controller asynchronously, otherwise the execution of controller is performed synchronously by invoking the Execute method.

   1:public interface IController
2: {
3: void Execute (RequestContext requestcontext);
4:}
5:public Interface Iasynccontroller:icontroller
6: {
7: IAsyncResult beginexecute (RequestContext RequestContext, AsyncCallback callback, object state);
8: void EndExecute (IAsyncResult asyncresult);
9:}

The controller type created by default through the Wizard of Visual Studio is a subclass of the abstract type controller. As shown in the following code fragment, controller implements both the IController and Iasynccontroller interfaces, so the controller is always executed asynchronously when the Mvchandler is requesting processing.

   1:public abstract class Controller:controllerbase, IController, Iasynccontroller, ...
2: {
3: //other Members
4: protected virtual bool Disableasyncsupport
5: {
6: Get{return false;
7: }
8:}

However, the controller type has a protected read-only property disableasyncsupport to indicate whether support for asynchronous execution is disabled. By default, this property value is false, so the asynchronous execution of controller is supported by default. If we set the value to true by overriding the property, then controller will execute only in a synchronized fashion. The specific implementation logic is embodied in the following code fragment: The BeginExecute method calls the Execute method when the Disableasyncsupport property is True (the method invokes a protected virtual method Executecore eventually synchronizes the controller Otherwise executes the controller asynchronously by calling Beginexecutecore/endexecutecore.

   1:public abstract class Controller: ...
2: {
3: //other Members
4: protected Virtual IAsyncResult beginexecute (RequestContext RequestContext,
5: AsyncCallback callback, Object state)
6: {
7: if (this. Disableasyncsupport)
8: {
9: //Execute controller synchronously by calling Execute method
: }
One: Else
: {
//asynchronous execution of controller by calling the Beginexecutecore/endexecutecore method
: }
15:}
: protected override void Executecore ();
protected Virtual IAsyncResult Beginexecutecore (AsyncCallback callback, object state);
: protected virtual void Endexecutecore (IAsyncResult asyncresult);
19:}

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.