Orchard Source Analysis (2): Orchard.Web.MvcApplication Class (Global)

Source: Internet
Author: User

Overviewanalysis of an ASP. NET project source, you can first browse its project structure, roughly a glimpse of the project's full picture , understand the dependencies between projects. Next you can browse the Web. config and Global.asax files to find the application entry point. This paper mainly analyzes the Global.asax file of Orchard Project, and the real analysis entry point in Global.asax codebehind file Global.asax.cs, the Orchard.Web.MvcApplication class (hereinafter referred to as the Mvcapplication Class). the Mvcapplication class handles three events start,beginrequest and endrequest. It is unclear whether the event, which is really famous as "Start", is so simple to understand. According to the Convention, these three events correspond to the Application_Start, Application_BeginRequest, and Application_EndRequest event handlers (methods), respectively, Of course, you can also register an event handler for another name by overriding the HttpApplication init method except--application_start.Start is a special event that is called only when the first resource in an ASP. NET application, such as an ASPX page, is requested. In the handler for this event (the Application_Start method), you can also use it only to set up static shared data during application startup. Orchard is primarily used to configure AUTOFAC containers, install extensions (modules and themes are collectively known as extensions), create and activate shells (subwebs). The beginrequest event occurs as the first event in an HTTP execution pipeline chain (pipeline) when ASP. NET responds to a request. The primary user in the orchard monitors the extension, and if the extension changes, the extension is reinstalled, the shell is recreated and activated, and the shell is reactivated if the shell status changes. The endrequest event occurs when ASP. NET responds to a request as the last event in an HTTP execution pipeline chain. Orchard is primarily used to process the tasks (task) in the processing engine (processingengine). First, Application_Start methodThe following code analysis toggles between the Mvcapplication class and the Orchard.warmupstarter.starter<t> class (hereinafter referred to as the Starter<t> Class). The Application_Start method first calls the RegisterRoutes method and adds a routing configuration that excludes requests to the suffix ". Axd" from the ASP. NET MVC processing pipeline. Here you will see the method inline: routetable. Routes.ignoreroute ("{resource}.axd/{*pathinfo}" ); The Application_Start method then uses the generic type parameter iorchardhost, calling the Starter<t> class with a constructor with three delegate arguments to create its instance and assigns it to the static private field _starter:_starter = new starter<iorchardhost > (hostinitialization, Hostbeginrequest, hostendrequest); Where the static private field _starter definition is defined in the Mvcapplication class:private static Starter< iorchardhost> _starter; Aside from the definition of iorchardhost and its role, it is only necessary to know that the Starter<t> class is responsible for creating an iorchardhost (Defaultorchardhost) instance.The Starter<t> class constructor accepts three delegate parameters. When creating objects of this type, the three parameters passed into the hstinitialization, Hostbeginrequest, and Hostendrequest are the three private static methods defined in the Mvcapplication class (which, of course, are packaged as delegates): iorchardhost hostinitialization (httpapplication application) void hostbeginrequest (httpapplication application, iorchardhost host) void hostendrequest (httpapplication application, iorchardhost host) below the Starter<t> class to see the definition of its constructor:          public starter (func < Span data-mce-= "" ><httpapplication action< Span data-mce-= "" ><httpapplication  action< Span data-mce-= "" ><httpapplication  < /span> _initialization = initialization;_beginrequest = beginrequest;_endrequest = endRequest;            }visible inside the constructor is simply an assignment operation that assigns the value of the parameter to the private delegate type field within the Starter<t> class. then back to the Application_Start method to continue the analysis. The Application_Start method then calls the _starter Onapplicationstart method, passing the current mvchttpapplication instance as a parameter: _starter. Onapplicationstart ( this); In -depth starter<t> Inside the Onapplicationstart method, the method starts a thread through the thread pool. The Hostinitialization method in the Mvchttpapplication class is eventually called in the new thread-the method that is wrapped by the delegate passed as a parameter to the Starter<t> constructor. For a specific analysis, refer to the next analysis of the Orchard.warmupstarter assembly. Here we look at the definition of the Hostinitialization method:private static iorchardhost hostinitialization ( httpapplication application) { var host = orchardstarter . CreateHost (mvcsingletons); host. Initialize (); //Initialize shells to speed up the first dynamic queryhost. BeginRequest ();host. EndRequest (); return host;     }the intent of the method is to create a iorchardhost (Defaultorchardhost) object and invoke several of its methods, and then return it. Here is not to delve into the role of iorchardhost these methods. second, Application_BeginRequest method and Application_EndRequest methodThese two methods are relatively simple, but simply call the corresponding method of the Starter<t> class: protected void application_beginrequest () { _starter. OnBeginRequest ( this);     } protected void application_endrequest () { _starter. Onendrequest ( this);     }the OnBeginRequest and Onendrequest methods of starter<t> invoke the Hostbeginrequest and Hostendrequest methods of Mvcapplication:Public void onbeginrequest (httpapplication application)     {        // ... (Omit the code to see the starter<t> class, which will be analyzed in the next article) //Only notify if the initialization have successfully completed if (_initializationresult! = null )         {_beginrequest (application, _initializationresult);        }    } Public void onendrequest (httpapplication application)     { //Only notify if the initialization have successfully completed if (_initializationresult! = null )         {_endrequest (application, _initializationresult);        }    }The Hostbeginrequest and Hostendrequest methods will eventually go to the call to the corresponding method of the Iorchardhost (defaultorchardhost): private static void hostbeginrequest ( httpapplication application, iorchardhost host) { application. context.items[ "Originalhttpcontext"] = Application. Context; host. BeginRequest ();    } private static void hostendrequest ( httpapplication application, iorchardhost host) { host. EndRequest ();    }Note that the Hostbeginrequest method saves the current request context (HttpContext) in Session state (Httpcontext.items). It was not difficult to see from above that a work to be done by Mvcapplication, which had been pushed to Starter<t>, which, after completing part of the latter, was transferred to Mvcapplication, and Mvcapplication proceeded with part of the process, Give the rest of the work to Iorchardhost (Defaultorchardhost) to complete. The starter<t> class is analyzed in detail when parsing the Orchard.warmupstarter assembly. The Iorchardhost (Defaultorchardhost) also has a dedicated space to analyze. Third, the question of mvchttpapplication analysis for the time being here, although it is just over 60 lines of code, but also include comments. But there are many questions left for us:1, the above although a number of mentions Iorchardhost (defaultorchardhost), but it is what, its role is what? 2. In the Mvcapplication.hostinitialization method, call the Orchardstarter.createhost method to create the Iorchardhost instance, how is it created? 3. In the Mvcapplication.hostinitialization method, why do I call iorchardhost.beginrequest and iorchardhost.endreques after calling the Iorchardhost.initialize method? T method? 4. Why should the current request context (HttpContext) be saved in Session state (Httpcontext.items) in the Mvcapplication.hostbeginrequest method? 5. What is Orchard.warmupstarter assembly for? 6, starter<t> in the task to return to the mvcapplication before the completion of those jobs? 7. How does the Mvcapplication.mvcsingletons method relate to how IOC/DI,AUTOFAC works? related type: orchard.warmupstarter.starter<t> Orchard.Environment.DefaultOrchardHost: Orchard.Environment.IOrchartHost orchard.environment. Orchardstarter References:Global.asax Detailedanother discussion on IIS and ASP .ASP. NET Compilation OverviewOverview of the ASP. NET application life cycle for IIS 5.0 and 6.0Overview of the ASP. NET application life cycle for IIS 7Understanding and deepening understanding of HttpApplication

Orchard Source Analysis (2): Orchard.Web.MvcApplication Class (Global)

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.