How do I deploy a managed HTTP Module Site Wide?

人走茶凉 提交于 2020-01-16 05:34:06

问题


I am developing a manged HTTP Module that will intercept requests to and response from IIS 7. The intercepted messages will be modified based on a set of business rules by a custom filter. The business rules will be stored in a configuration file.

The messages must be intercepted web site wide. This includes any applications or virtual directories that exist as children of the web site. My first attempt at this was to install the HTTP Module assembly in the bin directory of the desired web site.(e.g., C:\inetpub\wwwroot\bin for the Default Web Site).

Once installed I modify the <compilation> element of the web site's web.config file to reference the assembly, like so:

<compilation debug="false">
    <assemblies>
        <add assembly="Company.Product.Module, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx" />
    </assemblies>
</compilation>

I also modified the <modules> element of the web site's web.config file.

<system.webServer>
    <modules>
        <add name="MyModule" type="Company.Product.Module.MyModule" />
    </modules>
</system.webServer>

This works well for most content under the web site. However, if there is an application configured under the website (e.g., /wwwroot/MyApplication) I receive the following error when navigating to any resource under that web application:

Could not load file or assembly 'Company.Product.Module, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx' or one of its dependencies. The system cannot find the file specified.

There are two ways I know to get around this:

Option 1:

Copy the HTTP Module assembly and all dependent assemblies to each application's bin directory. I believe that I would also need to duplicate the configuration information from the parent directory. This can become a management nightmare as more and more applications are added to the web site.

Option 2:

Install the HTTP Module assembly and all dependent assemblies in the GAC. This seems to work quite well and avoids a lot of management overhead, however, where does configuration information live? If in the web site's web.config file is this information inherited in all the child applications?

What is the recommend method for deploying a managed HTTP Module site wide? How should configuration be handled so that all configuration is in a central location?


回答1:


Deploy the Module

Create a new directory under C:\Inetpub\Wwwroot named Module.
Create a subdirectory named Bin in the newly created Module directory. The resultant path is C:\Inetpub\Wwwroot\Module\Bin.
Copy MyModule.dll from your project's Bin\Debug directory to the C:\Inetpub\Wwwroot\Module\Bin directory.
Follow these steps to mark the new Module directory as a Web application:
    Open Internet Services Manager.
    Right-click the Module directory, and then click Properties.
    On the Directory tab, click Create.
    Click OK to close the Module Properties dialog box.

back to the top Configure the System

In the C:\Inetpub\Wwwroot\Module directory, create a new file named Web.config.
Paste the following text into Web.config:


<configuration>
   <system.web>
      <httpModules>
         <add name="MyModule" type="MyModule.SyncModule, MyModule" />
      </httpModules>
   </system.web>
</configuration>

back to the top Test the Module

In the C:\Inetpub\Wwwroot\Module directory, create a new .aspx file named Test.aspx.
Paste the following text into Test.aspx:


<%@Page Language="VB"%>
<% Response.Write("Hello from Test.aspx.<br>") %>


In the C:\Inetpub\Wwwroot\Module directory, create a Global.asax file.
Paste the following code in Global.asax:


<%@ Import Namespace="MyModule" %>

<script language="VB" runat=server >
Public Sub MyModule_OnMyEvent(src As Object, e As EventArgs)    
  Context.Response.Write("Hello from MyModule_OnMyEvent called in Global.asax.<br>")
End Sub
</script>


Request the Test.aspx page. You should see the following lines of text:


Hello from OnBeginRequest in custom module.
Hello from MyModule_OnMyEvent called in Global.asax.
Hello from Test.aspx.



回答2:


So far you are on right track, can you put your configs in machine.config? to avoid maintaining multiple config?




回答3:


You can GAC this dll, but it would break your x-copy deployment story if you already have one in place. If it is ok with you, you can later add this module to configuration in applicationHost.config in location tags: <location path="MySite">, <location path="MySite/MyApp">



来源:https://stackoverflow.com/questions/4109772/how-do-i-deploy-a-managed-http-module-site-wide

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!