简体   繁体   中英

IIS7 Installing/Configuring Native Module

I am learning to develop about creating a module for IIS to be use by web application. When I add my .dll to the /bin folder of web application that is hosted in IIS, it works.

But if I add this .dll to the root local server > Modules > Configure Native Modules and Register . It didn't work, when I run a web app the AppPool being use is stopped, the logs I'm seeing from the eventviewer is this:

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

This is my class that extends IHttpModule:

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>");
        }
    }
}

For anyone who is experiencing the same problem/issue. Lex Li's comment is true.

The class library I wrote is a managed module. That is not clear on almost all of the guide/tutorials I went to. So here's what I did to make it work:

  1. Register .dll to Global Assembly Cache (GAC)

    • To register .dll to GAC it must be signed. Go to your project's properties: Visual Studio > Solution Explorer > Right Click your project and Select Properties.
    • Go to Signing tab, check Sign the assembly.
    • Choose a strong name key file, select New
    • Enter your key file name
    • Untick Protect my key file with a password and click OK
    • Clean your project and Rebuild
  2. Use gacutility to register assembly to GAC (if you don't have gacutility.exe installed, you can download Windows SDK)

    • To register assembly to GAC, you can use VS Developer Command Prompt (run with admin rights) (if you don't have VS Developer Command Prompt you can use cmd with admin rights and change directory to location of gacutility.exe)
    • Confirm that your dll is not registered
    • gacutil /l MyModule.Test (assuming your dll is named MyModule.Test.dll)
    • To install: gacutil /i C:/path_to_dll/MyModule.Test.dll
    • Get public key token: gacutil /l MyModule.Test (take note of public key token we will use it to our web.config).
  3. Add the module in web app's 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> 

For anyone experiencing this error for NATIVE MODULES and most likely compiled in Release x64

Failed to find the RegisterModule entrypoint in the module DLL

While simply having the *.def file in your VC++ project seems to work for Win32 x86 compiled DLL's. When switching Configuration Manager to Release x64 compiled DLL modules (and removing the web config bitness32 in IIS), you may experience this error. You must include " /EXPORT:RegisterModule " in the Command Line options of the Linker under your project properties.

With my Native Module x64 DLL I went down this path for hours trying to figure out how to build my strongly named VC++ DLL; only to have SN.EXE keep telling me that my DLL

does not represent a strongly named assembly

I came to the conclusion that VC++ compiled x64 DLL's can't be strongly named and this was not the solution to my problem. Sometimes the error message is actually what the problem is, the RegisterModule wasn't being exported.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM