原本今天打算继续写ASP.NET MVC第四天的。但是由于里面涉及到asp.net运行机制的原理,如果不分析一下这里,mvc想说清楚还是挺困难的。既然要提到asp.net运行机制,所以打算还是说详细一点的好。记录mvc第一天的时候就说过,asp.net mvc也是基于asp.net运行机制的(也就是原理)。网上也有很多讲解asp.net运行机制的,我在这里说一下自己的认识,我们开始吧。
我们从web程序的入口开始。那就要先说到iis了,大家都知道,这是web服务软件。将web程序部署到iis过的人都知道,如果不做任何处理,我们写的webform是不能运行的。为什么非要执行aspnet_regiis才可以呢?我们看一下电脑路径C:\Windows\Microsoft.NET\Framework\v4.0.30319,aspnet_regiis.exe就在这里路径下。我们简单说一下原因,看下iis的历史,在百度上没有查到iis软件发布的年限,但至少iis在windows 2000的时候就存在了,而我们的.net framework在2002-02-13的时候才发布1.0版本,是啊,我们都知道微软很厉害,但就是在厉害他也不会强大到可以预测几年后的软件运行机制吧。也就是说iis对.net framework还说就是个“古董”,他不可能会知道.net framewrok运行机制,更不可能知道asp.net的运行机制。早起的iis也只能处理静态页面,也就类似于html,js,图片之类的东西。但现在如果我们想将asp.net 程序部署到iis上怎么办呢?对,扩展,使用扩展程序,我们运行aspnet_regiis.exe也就是将扩展程序(aspnet_isapi)注入到iis中,这样iis就可以处理了?---------哈哈,iis还是处理处理不了asp.net程序,但是后注入的程序可以告诉iis,我扩展程序可以处理什么样的程序,你如果处理不了,可以尝试交给我处理。
看一下上面说到的路经下面有个aspnet_isapi.dll,我们只是简单的说一下这里,这个dll是使用c/c++写的,并不是c#写的,所以我们无法反编译成c#代码。这是个承上启下的动态库,因为c/c++并不是我们考虑的范围内,我们直接认为这个程序将请求交给我们“在乎”的程序,下面我们开始反编译我们“在乎”程序。反编译工具中查找ISAPIRuntime这个类,下面是我反编译出来的结果这个类是system.web程序集下的类
1 public sealed class ISAPIRuntime : MarshalByRefObject, IISAPIRuntime, IISAPIRuntime2, IRegisteredObject 2 { 3 // Fields 4 private static int _isThisAppDomainRemovedFromUnmanagedTable; 5 private const int WORKER_REQUEST_TYPE_IN_PROC = 0; 6 private const int WORKER_REQUEST_TYPE_IN_PROC_VERSION_2 = 2; 7 private const int WORKER_REQUEST_TYPE_OOP = 1; 8 9 // Methods 10 [SecurityPermission(SecurityAction.Demand, Unrestricted=true)] 11 public ISAPIRuntime(); 12 [SecurityPermission(SecurityAction.LinkDemand, Unrestricted=true)] 13 public void DoGCCollect(); 14 public override object InitializeLifetimeService(); 15 [SecurityPermission(SecurityAction.LinkDemand, Unrestricted=true)] 16 public int ProcessRequest(IntPtr ecb, int iWRType); 17 internal static void RemoveThisAppDomainFromUnmanagedTable(); 18 [SecurityPermission(SecurityAction.LinkDemand, Unrestricted=true)] 19 public void StartProcessing(); 20 [SecurityPermission(SecurityAction.LinkDemand, Unrestricted=true)] 21 public void StopProcessing(); 22 [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] 23 void IISAPIRuntime2.DoGCCollect(); 24 [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] 25 int IISAPIRuntime2.ProcessRequest(IntPtr ecb, int iWRType); 26 [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] 27 void IISAPIRuntime2.StartProcessing(); 28 [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] 29 void IISAPIRuntime2.StopProcessing(); 30 void IRegisteredObject.Stop(bool immediate); 31 } 32 33 34 Expand Methods 35