Module registered in IIS7 doens't work

Soeren picture Soeren · Feb 9, 2012 · Viewed 9.6k times · Source

I've created a small Class Library, with a HttpModule that uses a filter to add some html to every requested page served by IIS7.

I tested it first by registering the module in the web.config in a test web site, and it works as it should, but only in that one application.

I generated a dll, and created a strong named assembly.

I need to somehow add this assembly as a module in IIS on a server level, so that it works for all requests, on all applications, and for non-asp.net content as well.

So far, I have tried adding the .dll as a native module. This doesn't work. It's on the list of native modules, but it doesn't work.

I have installed the .dll in the GAC.

Reading on, it seems I have to add the assembly as a managed module, and then choose it in the dropdown list under "add managed module" in IIS.

For this, I tried using the commandline tool appcmd, writing: "add module /name: string /type: string /preCondition: string"

I've had no success doing this, since I can't figure out what to set as type and precondition.

As I have read, the modules registered in IIS should work for all applications in all sites, and all requests.

The point is to avoid having to register the module in every applications web.config file.

Any ideas?

Answer

Soeren picture Soeren · Feb 20, 2012

After working with this for a bit, I managed to make it work.

Installing the assembly in the .net 4.0 GAC will not make it available in the type dropdown in IIS manager under "Add Managed Module".

What I had to do was:

Create the .net 4.0 Class Library, and compile it as a strong named assembly

Install it in the .net 4.0 GAC by using the gacutil, located in Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools

(Or make Visual Studio compile, sign and install the assembly automatically)

Add this line under <modules> in applicationHost.config: (it has to be done manually, it can't be done in the manager)

<add name="MyName" type="NameSpace.ClassName" preCondition="managedHandler,runtimeVersionv4.0" />

This makes the module run on requests to sites developed in .net 4.

It appears, however, that requests to sites developed in pre .net 4 versions can't use a module created in .net 4.0. So if you make requests for pages in a site created in .net 3.5, the module will not work.

Another observation:

After you have added the module to IIS via the applicationHost.config file, if you open the IIS manager, highlight the servername in connections and click modules. You will see the .net 4 module on the list.

Double click on it, and you'll then see the settings for it. You'll see that the "Invoke only for requests to ASP.NET applications or managed handlers" checkbox is checked. If you uncheck it, and hit ok, you'll get an error saying that the assembly hasn't been installed in the GAC.

But didn't I just install it succesfully in the .net 4 GAC? And didn't I just see the module run in a request?

If you go ahead and save the settings anyway, you get a runtime error, and if you look in applicationHost.config, you'll see that your previously manually added module settings has changed.

But what if I want to "Invoke only for requests to ASP.NET applications or managed handlers?