如何在站点范围内部署托管 HTTP 模块?
我正在开发一个托管 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将模块部署
回顶部
配置系统
返回顶部
测试模块
Deploy the Module
back to the top
Configure the System
back to the top
Test the Module
到目前为止,您的方向是正确的,您可以将配置放入 machine.config 中吗?避免维护多个配置?
So far you are on right track, can you put your configs in machine.config? to avoid maintaining multiple config?
您可以 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">