一、写在前面

  最近有时间,顺便将这系列洗完,接着上文:IIS各个版本知识总结

  这篇文章原本计划写到HttpHandler为止,但限于篇幅就写到httpmodule

  本文有不足之处,求指正,希望我能将它分析透彻.

二、回顾--我们还是从IIS说起

  从上文(IIS各个版本知识总结)可以很清楚地意思到经典模式和集成模式的区别:集成模式是一个伟大的改进,让IIS集成了.NET功能(不在依靠之前IIS版本的aspnet_ISPAI.DLL).

  所以,对于我们所开发的ASP.NET程序而言,这些完全不必知道;但运行环境自己必须知道发生了什么情况.

  所以两者的主要区别在于:从w3wp.exe初始化httpapplication这一阶段.

 

三、看看经典模式(包括IIS6)的过程

你必须知道ASP.NET知识------从IIS到httpmodule(第一篇)

IIS的处理进程到w3wp这一阶段后,iis会通过com的方式创建AppManagerAppDomainFactory对象,AppManagerAppDomainFactory会

在Create方法中创建ISAPIRuntime对象.代码如下.

using System;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
using System.Security.Permissions;
using System.Web.Util;
namespace System.Web.Hosting
{
    [SecurityPermission(SecurityAction.LinkDemand, Unrestricted = true)]
    public sealed class AppManagerAppDomainFactory : IAppManagerAppDomainFactory
    {
        private ApplicationManager _appManager;
        public AppManagerAppDomainFactory()
        {
            this._appManager = ApplicationManager.GetApplicationManager();
            this._appManager.Open();
        }
        [return: MarshalAs(UnmanagedType.Interface)]
        public object Create(string appId, string appPath)
        {
            object result;
            try
            {
                if (appPath[0] == '.')
                {
                    FileInfo fileInfo = new FileInfo(appPath);
                    appPath = fileInfo.FullName;
                }
                if (!StringUtil.StringEndsWith(appPath, '\\'))
                {
                    appPath += "\\";
                }
                ISAPIApplicationHost appHost = new ISAPIApplicationHost(appId, appPath, false);
                ISAPIRuntime iSAPIRuntime = (ISAPIRuntime)this._appManager.CreateObjectInternal(appId, typeof(ISAPIRuntime), appHost, false, null);
                iSAPIRuntime.StartProcessing();
                result = new ObjectHandle(iSAPIRuntime);
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }
        public void Stop()
        {
            this._appManager.Close();
        }
        internal static string ConstructSimpleAppName(string virtPath)
        {
            if (virtPath.Length <= 1)
            {
                return "root";
            }
            return virtPath.Substring(1).ToLower(CultureInfo.InvariantCulture).Replace('/', '_');
        }
    }
}
View Code

相关文章: