IIS7 Installing/Configuring 本机模块

IIS7 Installing/Configuring Native Module

我正在学习如何为 IIS 创建一个模块以供 Web 应用程序使用。当我将我的 .dll 添加到 IIS 中托管的 Web 应用程序的 /bin 文件夹时,它可以工作。

但是如果我将此 .dll 添加到 root 本地服务器 > Modules > Configure Native Modules 注册 。它没有用,当我 运行 正在使用 AppPool 的 Web 应用程序停止时,我从事件查看器中看到的日志是这样的:

Failed to find the RegisterModule entrypoint in the module DLL %windir%\TestWebAgent\x86\TestModule.dll. The data is the error.

这是我扩展 IHttpModule 的 class:

using System;
using System.Web;

namespace MyTestModule
{
    public class TestModule : IHttpModule
    {
        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void Init(HttpApplication application)
        {
            application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
        }

        private void Application_BeginRequest(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;

            context.Response.Write("<h1><font color=green>AUTHENTICATED</font></h1><hr>");
        }
    }
}

对于有同样经历的人problem/issue。 Lex Li 的评论是真实的。

我写的class库是一个托管模块。说不清楚就几乎所有的guide/tutorials我都去了。所以这就是我为让它发挥作用所做的工作:

  1. 将 .dll 注册到全局程序集缓存 (GAC)

    • 要将 .dll 注册到 GAC,必须对其进行签名。转到您项目的属性:Visual Studio > 解决方案资源管理器 > 右键单击​​您的项目和 Select 属性。
    • 转到“签名”选项卡,选中“对程序集签名”。
    • 选择强名称密钥文件,select新建
    • 输入您的密钥文件名
    • 取消勾选使用密码保护我的密钥文件并单击确定
    • 清理您的项目并重建
  2. 使用gacutility注册程序集到GAC(如果没有安装gacutility.exe,可以下载WindowsSDK)

    • 要将程序集注册到 GAC,您可以使用 VS 开发人员命令提示符(运行 具有管理员权限)(如果您没有 VS 开发人员命令提示符,您可以使用具有管理员权限的 cmd 并将目录更改为gacutility.exe)
    • 的位置
    • 确认你的dll没有注册
    • gacutil /l MyModule.Test(假设你的dll被命名为MyModule.Test.dll)
    • 安装:gacutil /i C:/path_to_dll/MyModule.Test.dll
    • 获取public密钥令牌:gacutil /l MyModule.Test(记下public密钥令牌,我们将在web.config中使用它)。
  3. 在网络应用程序中添加模块 web.config:

    <system.webServer>
        <modules>
          <add name="MyModule" type="Namespace.ClassName, ProjectName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=999cf99ff999a99e, processorArchitecture=MSIL" preCondition="managedHandler,runtimeVersionv4.0" />
        </modules>
    </system.webServer>
    

针对 NATIVE MODULES 并且很可能在版本 x64

中编译时遇到此错误的任何人

Failed to find the RegisterModule entrypoint in the module DLL

虽然在您的 VC++ 项目中简单地包含 *.def 文件似乎适用于 Win32 x86 编译的 DLL。将 Configuration Manager 切换到 Release x64 编译的 DLL 模块(并在 IIS 中删除 Web 配置 bitness32)时,您可能会遇到此错误。 您必须在项目属性下的链接器的命令行选项中包含“/EXPORT:RegisterModule”。

使用我的本机模块 x64 DLL,我沿着这条路走了几个小时,试图弄清楚如何构建我的强命名 VC++ DLL;只是 SN.EXE 一直告诉我我的 DLL

does not represent a strongly named assembly

我得出的结论是 VC++ 编译的 x64 DLL 不能被强命名,这不是我的问题的解决方案。 有时错误消息实际上就是问题所在,RegisterModule 没有被导出。