IIS 8.5 中的 HttpModule 未加载

2024-03-07

我用 C# 为 IIS 8.5 编写了一个简单的托管 HttpModule,并将其安装到全局程序集缓存中(CLR 版本 4.0.30319)。 IIS 检测到它存在,并且我已将其作为应用程序主机级别的模块安装。

不幸的是,它似乎根本没有被执行。我们只提供静态内容,但由于 IIS 以集成模式运行,我的印象是 HttpModule 是针对所有请求执行的,而不仅仅是 ASP.NET 管道中的请求。

HttpModule 已简化到尽可能简单的级别 - 记录模块创建、处置、请求开始和请求结束的时间,如下所示:

using System;
using System.Web;
using System.Collections.Specialized;
using System.IO;

public class ServerLoggingModule : IHttpModule
{
    public ServerLoggingModule()
    {
       using (StreamWriter file = new StreamWriter(@"C:\LocalIISAuth\logs\iis.log"))
       {
           file.WriteLine("Created module");
       }
    }

    public string ModuleName
    {
        get { return "ServerMaskModule"; }
    }

    public void Dispose()
    {
        using (StreamWriter file = new StreamWriter(@"C:\LocalIISAuth\logs\iis.log"))
        {
            file.WriteLine("Disposed of module");
        }    
    }

    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(ServerLoggingModule.BeginRequestHandler);
        context.EndRequest += new EventHandler(ServerLoggingModule.EndRequestHandler);
    }

    protected static void BeginRequestHandler(Object sender, EventArgs e)
    {
        using (StreamWriter file = new StreamWriter(@"C:\LocalIISAuth\logs\iis.log"))
        {
            file.WriteLine("BeginRequest");
        }
    }

    protected static void EndRequestHandler(Object sender, EventArgs e)
    {
        using (StreamWriter file = new StreamWriter(@"C:\LocalIISAuth\logs\iis.log"))
        {
            file.WriteLine("EndRequest");
        }
    }
}

请求到达模块所在管道中的点,但似乎只是跳过该模块。没有显示任何错误,页面显示正常。

提前致谢


事实证明,我们没有启用 .NET 可扩展性 Windows 功能,导致模块无法加载。不幸的是,IIS 确实允许您向其中添加托管模块,尽管它肯定知道它永远无法启动它!

要使用 PowerShell 添加这些模块,请使用:

Install-WindowsFeature Web-Net-Ext
Install-WindowsFeature Web-Net-Ext45 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

IIS 8.5 中的 HttpModule 未加载 的相关文章

随机推荐