2

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

不幸的是,它似乎根本没有被执行。我们只提供静态内容,但由于 IIS 在集成模式下运行,我的印象是 HttpModules 是针对所有请求执行的,而不仅仅是 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");
        }
    }
}

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

提前致谢

4

2 回答 2

3

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

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

Install-WindowsFeature Web-Net-Ext
Install-WindowsFeature Web-Net-Ext45 
于 2015-05-05T12:58:18.643 回答
-1

有时池会以某种方式损坏,重新创建池可以解决此类问题。希望它可以帮助某人。

于 2018-08-16T11:00:54.723 回答