A module registered in IIS7 does not work

I created a small class library with an HttpModule that uses a filter to add some html to every requested page served by IIS7.

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

I created a dll and created a strong named assembly.

I need to somehow add this assembly as a module in IIS at the server level so that it works for all requests, for all applications, as well as for nonasp.net content.

So far, I have been trying to add .dll as a native module. This does not work. This is on the list of native modules, but it does not work.

I installed .dll in the GAC.

Reading, I think I need to add the assembly as a managed module, and then select it from the drop-down list in the "Add a managed module" section in IIS.

To do this, I tried using the appcmd command-line tool by writing: "add module / name: string / type: string / preCondition: string"

I did not succeed, since I cannot understand what needs to be specified as a type and a precondition.

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

The point is not to register a module in every web.config application file.

Any ideas?

+6
source share
3 answers

Having worked with this a bit, I managed to get it to work.

Installing an assembly in .net 4.0 GAC will not make it available in the drop-down list of types in IIS Manager under the "Add a managed module" section.

What I needed to do:

Create a .net 4.0 class library and compile it as a strong named assembly

Install it in the .net 4.0 GAC using gacutil located in the program files (x86) \ Microsoft SDK \ 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: (this needs to be done manually, this cannot be done in the manager)

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

This forces the module to launch requests to sites developed in .net 4.

It seems, however, that requests for sites developed in pre.net 4 versions cannot use the module created in .net 4.0. Therefore, if you make requests for pages on a site created in .net 3.5, the module will not work.

Another observation:

After you have added the module to IIS through the applicationHost.config file, if you open IIS Manager, highlight the server name in the connections and click the modules. You will see the .net 4 module in the list.

Double click on it and you will see the settings for it. You will see that the "Call only for requests to ASP.NET applications or managed handlers" checkbox is selected. If you remove it and click "OK", you will receive an error message that the assembly is not installed in the GAC.

But haven't I just installed it successfully in .net 4 GAC? And don't I see how the module starts in the request?

If you save the settings anyway, you will get a runtime error, and if you look in applicationHost.config, you will see that your module settings you added earlier manually have changed.

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

+6
source

Now I can run the module for each request. The reason she hadn't worked before was a completely unrelated mistake on my part. So, the steps necessary for its work:

  • Write the code that you want to run on every request in the form of a .net 3.5 class library.
  • Compile it as an assembly with a strong name.
  • Install the assembly in the GAC.
  • In IIS7 Manager, select the server name in the connections, click "Modules", click "Add a managed module" in action.
  • write the name of the module and select a new assembly from the drop-down list.
  • Make sure that the site uses an integrated application pool.

Of course, it is not always recommended to skip all the code in all requests, so you may need to filter out some of the requested files.

One question remains!

There are currently two GACs, Microsoft.NET for .NET 4.0 and Windows GAC for pre.net 4.0. Since I created my assembly in .net 3.5, it was installed on the Windows GAC, and therefore it was available in the dropdown list of types in IIS Manager.

When I created my assembly in .net 4.0, it was installed in the Microsoft.NET GAC, and as a result, it was not available in the drop-down list of types in IIS Manager.

Question: How do you add .net 4.0 assembly as a managed module in IIS7 and does it run like my 3.5 managed module?

It should be possible, right?

+5
source

You need to add the module at the server level. You can do this from the command line:

 appcmd add module /name:string /type:string /preCondition:string 

To execute a command line command: appcmd add module /?
In short, it should look like this:

 appcmd add module /name:AnyNameOfYourChoice /type:YourClassNameSpace.YourClassName 
Parameter

/preContition is optional.

More details here .

OR

Do this from IIS Manager by going to the node server → Modules → Add Managed Module
More details here . (at the bottom of the page)

+2
source

Source: https://habr.com/ru/post/908036/


All Articles