Search code examples
c#.netiishttpmoduleiis-8.5

HttpModule in IIS 8.5 not being loaded


I've written a simple managed HttpModule for IIS 8.5 in C# and have installed this into the global-assembly cache (CLR version 4.0.30319). This is detected as present by IIS and I've installed it as a module at the application host level.

Unfortunately it doesn't seem to be executed at all. We're only serving up static content, but since IIS in running in integrated mode I was under the impression that HttpModules were executed for all requests, not just those in the ASP.NET pipeline.

The HttpModule has been reduced to the simplest possible level - logging when the module is created, disposed of, a request begins and a request ends, as below:

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

The request reaches the point in the pipeline the module resides at, but just seems to skip the module. There are no errors displayed and the page displays fine.

Thanks in advance


Solution

  • As it turns out we hadn't enabled the .NET extensibility Windows features which resulted in the module not being loaded. Unfortunately IIS does let you add the managed module to it, despite it surely knowing it would never be able to launch it!

    To add these modules with PowerShell use:

    Install-WindowsFeature Web-Net-Ext
    Install-WindowsFeature Web-Net-Ext45