首先请求是怎么样进入httpruntime的,iis6.0中,又http.sys监听80端口,当请求过来时http.sys去访问注册表看这个请求由谁来处理,查询后一般是交给iis处理,iis服务器上寄存着inetinfo.exe这个进程,这个进程中存放着matebase(元数据),元数据中有应用程序池到workprocess之间的映射,因此iis就有了这几个职责:第一,创建应用程序池(这个应该是在http.sys中创建的)。第二,维持应用程序池到workprocess之间的映射。第三,判断请求应该由谁来处理(判断后缀.aspx,*.*)由aspnet_isapi.dll处理(由映射来处理就可以了,这个好像也没有)。根据这个Mapping,WAS(Web Administrative service)将存在于某个application pool queue 的request传递到对应的worker process中,如果还没有这个worker process就创建这样一个进程(w3wp.exe进程中被创建)。在worker process被初始化的时候,加载aspnet isapi,aspnet isapi进而加载CLR
CLR加载时先从AppManagerAppDomainFactory执行,这个类首先要从IAppManagerAppDomainFactory接口入手
1 [ComImport, Guid("02998279-7175-4d59-aa5a-fb8e44d4ca9d"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] 2 public interface IAppManagerAppDomainFactory 3 { 4 [return: MarshalAs(UnmanagedType.Interface)] 5 [SecurityPermission(SecurityAction.LinkDemand, Unrestricted=true)] 6 object Create([In, MarshalAs(UnmanagedType.BStr)] string appId, [In, MarshalAs(UnmanagedType.BStr)] string appPath); 7 [SecurityPermission(SecurityAction.LinkDemand, Unrestricted=true)] 8 void Stop(); 9 }
IAppManagerAppDomainFactory接口定义了两个方法一个Create方法和stop方法,需要子类去实现,那么谁来实现这个接口呢?当然是AppManagerAppDomainFactory类
1 using System; 2 using System.Collections.Generic; 3 using System.Globalization; 4 using System.IO; 5 using System.Linq; 6 using System.Runtime.InteropServices; 7 using System.Runtime.Remoting; 8 using System.Security.Permissions; 9 using System.Text; 10 using System.Threading.Tasks; 11 12 namespace System.Web.Hosting 13 { 14 [SecurityPermission(SecurityAction.LinkDemand, Unrestricted = true)] 15 public sealed class AppManagerAppDomainFactory : IAppManagerAppDomainFactory 16 { 17 // Fields 18 //定义ApplicationManager 19 private ApplicationManager _appManager; 20 21 // Methods 22 public AppManagerAppDomainFactory() 23 { 24 this._appManager = ApplicationManager.GetApplicationManager(); 25 this._appManager.Open(); 26 } 27 internal static string ConstructSimpleAppName(string virtPath) 28 { 29 if (virtPath.Length <= 1) 30 { 31 return "root"; 32 } 33 return virtPath.Substring(1).ToLower(CultureInfo.InvariantCulture).Replace('/', '_'); 34 } 35 [return: MarshalAs(UnmanagedType.Interface)] 36 public object Create(string appId, string appPath) 37 { 38 object obj2; 39 try 40 { 41 if (appPath[0] == '.') 42 { 43 FileInfo info = new FileInfo(appPath); 44 appPath = info.FullName; 45 } 46 if (!StringUtil.StringEndsWith(appPath, '\')) 47 { 48 appPath = appPath + @""; 49 } 50 ISAPIApplicationHost appHost = new ISAPIApplicationHost(appId, appPath, false); 51 ISAPIRuntime o = (ISAPIRuntime)this._appManager.CreateObjectInternal(appId, typeof(ISAPIRuntime), appHost, false, null); 52 o.StartProcessing(); 53 obj2 = new ObjectHandle(o); 54 } 55 catch (Exception) 56 { 57 throw; 58 } 59 return obj2; 60 } 61 public void Stop() 62 { 63 this._appManager.Close(); 64 } 65 } 66 }
AppManagerAppDomainFactory中的构造函数已经写出来了,同样也实现了create和stop方法