【问题标题】:How do I deploy a managed HTTP Module Site Wide?如何在站点范围内部署托管 HTTP 模块?
【发布时间】:2023-03-16 13:50:01
【问题描述】:

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

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

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

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

我还修改了网站 web.config 文件的 &lt;modules&gt; 元素。

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

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

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

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

选项 1:

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

选项 2:

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

在站点范围内部署托管 HTTP 模块的推荐方法是什么?应该如何处理配置,以便所有配置都在一个中心位置?

【问题讨论】:

    标签: .net iis-7 httpmodule .net-3.5 .net-4.0


    【解决方案1】:

    部署模块

    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.
    

    【讨论】:

    • 你使用拦截器修改http请求和响应。请求修改标头以传递令牌等授权详细信息。并为您处理响应错误的响应。
    【解决方案2】:

    您可以 GAC 这个 dll,但如果您已经有一个,它会破坏您的 x-copy 部署故事。如果您没问题,您可以稍后将此模块添加到 applicationHost.config 中位置标签的配置中:&lt;location path="MySite"&gt;, &lt;location path="MySite/MyApp"&gt;

    【讨论】:

    • 我不确定是否允许我修改 applicationHost.config 文件。此外,我希望能够支持不同网站的不同配置。因此,我认为此信息的理想位置是在 web.config 中。我需要调查一下我的配置设置是否在站点内的所有应用程序中正确继承。
    【解决方案3】:

    到目前为止,您走在正确的轨道上,您可以将您的配置放在 machine.config 中吗?避免维护多个配置?

    【讨论】:

    • 在正确的轨道上您是指在 GAC 中安装,还是在多个 bin 目录中安装?
    • 我不确定是否允许我修改 machine.config 文件。此外,我希望能够支持不同网站的不同配置。因此,我认为此信息的理想位置是在 web.config 中。我需要调查一下我的配置设置是否在站点内的所有应用程序中正确继承。
    猜你喜欢
    • 1970-01-01
    • 2019-02-09
    • 2019-10-14
    • 2019-10-01
    • 1970-01-01
    • 2011-04-29
    • 1970-01-01
    • 2011-10-26
    • 2020-05-30
    相关资源
    最近更新 更多