我用 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");
}
}
}
请求到达模块所在的管道中的点,但似乎只是跳过了模块。没有显示错误,页面显示正常。
提前致谢