Search code examples
c#iisiis-modules

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

Solution

  • 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>