MVC系列(2) HttpRuntime详解分析(上)

文章内容

从上章文章都知道,asp.net是运行在HttpRuntime里的,但是从CLR如何进入HttpRuntime的,可能大家都不太清晰。本章节就是通过深入分析.Net4的源码来展示其中的重要步骤。请先看下图:

首先,CLR在初始化加载的时候,会加载一个非常重要的类AppManagerAppDomainFactory,这个类是做什么用的呢?首先这个类继承了IAppManagerAppDomainFactory接口,而这个接口是是有个可供COM调用的Create方法,代码如下:

[ComImport, Guid("02998279-7175-4d59-aa5a-fb8e44d4ca9d"), System.Runtime.InteropServices.InterfaceTypeAttribute(System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIUnknown)]  
    public interface IAppManagerAppDomainFactory {   
#if !FEATURE_PAL // FEATURE_PAL does not enable COM   
        [return: MarshalAs(UnmanagedType.Interface)]  
#else // !FEATURE_PAL   
        Object Create(String appId, String appPath);  
#endif // !FEATURE_PAL  
      
        Object Create([In, MarshalAs(UnmanagedType.BStr)] String appId,   
                      [In, MarshalAs(UnmanagedType.BStr)] String appPath);  
       
       
        void Stop();  
    }

我们来细看一下这个AppManagerAppDomainFactory是如何实现这个接口的,首先该类在默认的构造函数里,获取了一个ApplicationManager的实例用于在Create方法里使用。代码如下:

[SecurityPermission(SecurityAction.Demand, Unrestricted=true)]  
      
public AppManagerAppDomainFactory() {  
      
    _appManager = ApplicationManager.GetApplicationManager();  
      
    _appManager.Open();  
      
}

回到实现接口的Create方法,我们来看最重要的3行代码:

ISAPIApplicationHost appHost = new ISAPIApplicationHost(appId, appPath, false /*validatePhysicalPath*/);  
      
       
      
ISAPIRuntime isapiRuntime = (ISAPIRuntime)_appManager.CreateObjectInternal(appId, typeof(ISAPIRuntime), appHost,  
      
        false /*failIfExists*/, null /*hostingParameters*/);  
      
      
isapiRuntime.StartProcessing();




转载请注明:http://www.shhjfk.com/pxxx/pxxx/20.html