如何在站点范围内部署托管 HTTP 模块?

发布于 2024-09-30 16:17:53 字数 1402 浏览 5 评论 0原文

我正在开发一个托管 HTTP 模块,它将拦截对 IIS 7 的请求和响应。拦截的消息将由自定义过滤器根据一组业务规则进行修改。业务规则将存储在配置文件中。

这些消息必须在整个网站范围内被拦截。这包括作为网站子级存在的任何应用程序或虚拟目录。我的第一次尝试是将 HTTP 模块程序集安装在所需网站的 bin 目录中。(例如,默认网站为 C:\inetpub\wwwroot\bin)。

安装后,我修改网站 web.config 文件的 元素以引用程序集,如下所示:

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

我还修改了 元素网站的 web.config 文件。

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

这对于网站下的大多数内容都很有效。但是,如果网站下配置了应用程序(例如 /wwwroot/MyApplication),则在导航到该 Web 应用程序下的任何资源时,我会收到以下错误:

无法加载文件或程序集 '公司.产品.模块, 版本=1.0.0.0,文化=中立, PublicKeyToken=xxxxxxxxxxxxxxx' 或 它的依赖项之一。系统 找不到指定的文件。

我知道有两种方法可以解决此问题:

选项 1:

将 HTTP 模块程序集和所有依赖程序集复制到每个应用程序的 bin 目录。我相信我还需要从父目录复制配置信息。随着越来越多的应用程序添加到网站,这可能会成为管理的噩梦。

选项 2

在 GAC 中安装 HTTP 模块程序集和所有依赖程序集。这似乎工作得很好,并且避免了大量的管理开销,但是,配置信息位于哪里?如果在网站的 web.config 文件中,所有子应用程序都会继承此信息吗?

在站点范围内部署托管 HTTP 模块的推荐方法是什么?应如何处理配置以使所有配置都位于中央位置?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事和酒 2024-10-07 16:17:53

将模块部署

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.

回顶部
配置系统

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>

返回顶部
测试模块

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.

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.
酒解孤独 2024-10-07 16:17:53

到目前为止,您的方向是正确的,您可以将配置放入 machine.config 中吗?避免维护多个配置?

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

反差帅 2024-10-07 16:17:53

您可以 GAC 这个 dll,但如果您已经有一个 dll,它会破坏您的 x-copy 部署方案。如果您同意,您可以稍后将此模块添加到 applicationHost.config 中位置标记的配置中: ,< /代码>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文