asp. NET MVC is a more advanced framework that is currently used by more web Frameworks. I'm going to say my own understanding of the principles of ASP and some practical experience based on my actual project experience and the relevant source code and some excellent projects using asp. net MVC (mainly orchard). At present, most of the source code for. NET is open, which greatly facilitates our analysis of asp. the following is the basic process of HTTP requests coming into the MVC framework before Processing.
Because of the different versions of IIS and the mode of Operation (classic mode, integrated mode), HTTP requests are not the same as the processing channels for asp. scrutiny the details inside, starting with the creation of the application domain:
Appmanagerappdomainfactory Analysis
Note: appdomainfactory and Appmanagerappdomainfactory classes are implemented in System.Web.Hosting
The Iappdomainfactory interface is called when the AppDomain is created, and the implementation of this interface is as Follows:
public sealed class appdomainfactory:iappdomainfactory { private appmanagerappdomainfactory _realfactory ; Public appdomainfactory () { _realfactory = new appmanagerappdomainfactory (); } Public Object Create (string module, string typeName, string appId, string apppath,string strurlofapporigin, int i Zone) {return _realfactory.create (appId, appPath);}}
The implementation invokes appmanagerappdomainfactory to complete the actual creation Process.
public sealed classAppmanagerappdomainfactory:iappmanagerappdomainfactory {privateApplicationmanager _appmanager; Public appmanagerappdomainfactory () {_appmanager = Applicationmanager.getapplicationmanager (); _appmanager.open (); Public Object Create (string appId, string AppPath) {try {if (apppath[0] = = '. ') ) {System.IO.FileInfo file = new System.IO.FileInfo (appPath); appPath = file. FullName; } If (! Stringutil.stringendswith (appPath, ' \ \ ' )) {appPath = appPath + "\ \" ;} Isapiruntime isapiruntime = (isapiruntime) _appmanager.createobjectinternal (appId, typeof (isapiruntime), apphost,false, NULL ); Isapiruntime.startprocessing (); return new objecthandle (isapiruntime),} catch (Exception e) {debug.trace ("internal", "appdomainfactory:: Create failed with "+ E.gettype (). FullName + ":" + e.message + "\ r \ n" + e.stacktrace); Throw ;}} }
The main function of code is to create an AppDomain through Applicationmanager createobjectinternal, create hostingenvironment, etc., and finally get an instance of isapiruntime, Then let the unmanaged code Call.
Applicationmanager Analysis
Note: the Applicationmanager class is implemented in System.Web.Hosting
First look at the static method of creating the Applicationmanager object in the Applicationmanager class:
public static Applicationmanager Getapplicationmanager () { if (_theappmanager = = null) { lock (_ Applicationmanagerstaticlock) { if (_theappmanager = = null) { if (hostingenvironment.ishosted) _ Theappmanager = Hostingenvironment.getapplicationmanager (); if (_theappmanager = = null) _theappmanager = new< c12> Applicationmanager (); }}} return _theappmanager;}
If Hostingenvironment has already been created, you can return the Applicationmanager of the current hostingenvironment setting directly, and if not, create a new Applicationmanager instance.
Then look at Applicationmanager's createobjectinternal method:
Internal IRegisteredObject createobjectinternal (String appId, type type, iapplicationhost appHost, bool failifexists, hostingenvironmentparameters hostingparameters) { if (!typeof(iregisteredobject). IsAssignableFrom (type)) throw new ArgumentException (SR. GetString (SR. not_iregisteredobject, Type. FullName), "type"); Hostingenvironment env = getappdomainwithhostingenvironment (appId, appHost, hostingparameters); ObjectHandle h = Env. Createwellknownobjectinstance (type. assemblyqualifiedname, failifexists); return (h! = null)? H.unwrap () as Iregisteredobject:null;}
The first step is to obtain an instance of hostingenvironment, and then return the instance of Isapiruntime required by the Create method through the Createwellknownobjectinstance method of the Instance. Let's start by looking at how to get the Hostingenvironment instance Getappdomainwithhostingenvironment.
Private hostingenvironment getappdomainwithhostingenvironment (String appId, iapplicationhost appHost, Hostingenvironmentparameters hostingparameters) { Lockableappdomaincontext ac = getlockableappdomaincontext (appId); Lock (ac) { hostingenvironment env = AC. hostenv; If (env! = null) { try {env. isunloaded (); } catch(appdomainunloadedexception) {env = null;}} if (env = = null) {env = Createappdomainwithhost Ingenvironmentandreporterrors (appId, appHost, hostingparameters); Ac. hostenv = env; Interlocked.Increment (ref _accessiblehostingenvcount);} return env;}}
first, The dictionary will be checked to see if there is an existing hostingenvironment instance, and if so, a new one will be created and saved in the Dictionary. Viewing the associated code discovery eventually calls Applicationmanager's private method createappdomainwithhostingenvironment to create the AppDomain and Hostingenvironment.
PrivateHostingenvironment createappdomainwithhostingenvironment (String appId, iapplicationhost appHost, Hostingenvironmentparameters hostingparameters) {... AppDomain =Appdomain.createdomain (domainid,getdefaultdomainidentity (), setup); ...... Type HostType = typeof (hostingenvironment); String module = hostType.Module.Assembly.FullName; String typeName = hosttype.fullname; ObjectHandle h = null ... try {h = activator.createinstance (appDomain, module, typeName);} ... Hostingenvironment env = (h! = null)? H.unwrap () as Hostingenvironment:null ; if (env = = null ) throw new SystemException (SR. GetString (SR. cannot_create_hostenv)); Iconfigmappathfactory configmappathfactory = apphost.getconfigmappathfactory (); Appdomainstartupconfigurationexception = = NULL ) {env. Initialize (this , appHost, configmappathfactory, hostingparameters, policylevel);} Else {env. Initialize (this , appHost, configmappathfactory, hostingparameters, policylevel, appdomainstartupconfigurationexception); } return env;}
You can see that the code creates the AppDomain and the Hostingenvironment instance, and after the hostingenvironment instance is created, it immediately calls its Initialize method to initialize and then returns the object Instance.
Hostingenvironment Analysis
Initialize initialization method, Note that the first parameter of the method is this, which is the Applicationmanager instance itself, which sets the Applicationmanager in Hostingenvironment. Thus the applicationmanager can be obtained by hostingenvironment after Initialization. The Initialize method invokes the static method of httpruntime, doing some initialization (where the Initializebuildmanager method of BuildManager is called to initialize some other work, including compiling APP_ All. NET source code in the Code directory). finally, if the hostingenvironment initialization fails, The hostinginitfailed is set to True.
internal void Initialize (applicationmanager appmanager, iapplicationhost appHost, iconfigmappathfactory configmappathfactory, hostingenvironmentparameters hostingparameters, policylevel PolicyLevel, Exception Appdomaincreationexception) { ... _appmanager = appmanager; ... Httpruntime.initializehostingfeatures (hostingflags,policylevel,appdomaincreationexception); ... Catch (Exception e) { _hostinginitfailed = True; }}
When Hostingenvironment is created, its method createwellknownobjectinstance is called to create the Isapiruntime. After you have created the Isapiruntime instance, the initialization work completes the first phase, as shown in the Following:
MVC prior To-asp.net initialization process Analysis 1