MVC series (4) Http Pipeline detailed analysis (i)

Source: Internet
Author: User
Tags httpcontext

Continue with the contents of the previous chapter, get the instance through the HttpApplicationFactory getapplicationinstance static method, and execute the BeginProcessRequest method of the instance to perform the remaining HTTP Pipeline operation, the code is as follows:

Get application Instance
IHttpHandler app = httpapplicationfactory.getapplicationinstance (context);

So what did getapplicationinstance do with this method? Is it just a new object? It doesn't feel like it, so let's take a look at the HttpApplicationFactory class GetApplicationInstance static method source:

Internal static IHttpHandler GetApplicationInstance (HttpContext context) {   
    if (_customapplication!= null)  
        return _customapplication;   
       
    Check to the If it's a debug Auto-attach request  
    if (context). Request.isdebuggingrequest) return to   
        new Httpdebughandler ();  
      
    _theapplicationfactory.ensureinited ();  
       
    _theapplicationfactory.ensureappstartcalled (context);  
       
    return _theapplicationfactory.getnormalapplicationinstance (context);   
}

There are 3 lines of code in it. I've already marked it as bold, and before I explain the specifics of each line of code, let's look at where the _theapplicationfactory object instance comes from and see it as a singleton implementation by looking at the declaration code for that field.

The only instance of application factory  
private static httpapplicationfactory _theapplicationfactory = new Httpapp Licationfactory ();

The first line of bold code is execution, the ensureinited method of the instance, which invokes the Init method by lock (the benefit is not to be added), the code reads as follows:

private void ensureinited () {  
    if (!_inited) {  
        Lock (this) {   
            if (!_inited) {  
                Init ();   
                _inited = true;   
            }}}  

By looking at the code for the Init method and the details in 2 lines of the following code, we can tell that the 2 lines of code are essentially getting the content from Global.asax and then compiling it.

_appfilename = Getapplicationfile ();
CompileApplication ();

So, the Httpapplicationfactory._theapplicationfactory.ensureinited () method first checks whether the httpapplicationfactory is initialized, and if not, is initialized by Httpapplicationfactory.init (). In Init (), first get the full path to the Global.asax file, and then call CompileApplication () to compile the global.asax.

The 2nd line of bold ensureappstartcalled method will eventually invoke the following private method Fireapplicationonstart, the code is as follows:

private void Fireapplicationonstart (HttpContext context) {   
    if (_onstartmethod!= null) {   
        HttpApplication app = Ge Tspecialapplicationinstance ();  
       
        App. Processspecialrequest (Context  
                                    ,  
                                    _onstartmethod,  
                                    _onstartparamcount, this   
                                    ,  
                                    Eventargs.empty,   
                                    null);   
      
        Recyclespecialapplicationinstance (app);   
    }  

Through the code we can learn that httpapplicationfactory._theapplicationfactory.ensureappstartcalled (context) creates specific HttpApplication instances, Triggers the Applicationonstart event, executing the Application_Start (object sender, EventArgs e) method in Asp.global_asax. It is then reclaimed immediately after the event is processed, because the system initialization only takes one time, but the getspecialapplicationinstance in it will do something special for IIS7, as we'll discuss later in the chapter.

The bold code in line 3rd is the key point here, and the code in the method is as follows:

Private HttpApplication Getnormalapplicationinstance (HttpContext context) {  
    HttpApplication app = null;   
      
    Lock (_freelist) {  
        if (_numfreeappinstances > 0) {  
            app = (HttpApplication) _freelist.pop ();   
            _numfreeappinstances--;  
       
            if (_numfreeappinstances < _minfreeappinstances) {   
                _minfreeappinstances = _numfreeappinstances;  
            }   
        }  
    }  
      
    if (app = null) {   
        //if ran out of instances, create a new one  
        app = (HttpApplication) httpruntime.createn Onpublicinstance (_theapplicationtype);   
       
        using (new Applicationimpersonationcontext ()) {  
            app. Initinternal (context, _state, _eventhandlermethods);   
        }  
      
    return app;   
}

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/aspx/

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.