HttpModule 似乎不起作用
我创建了一个简单的 HttpModule 来记录现有 Web 服务的使用情况。 有一个包含单个类的 dll
public class TrackingModule : System.Web.IHttpModule
{
public TrackingModule(){}
public void Init(System.Web.HttpApplication context)
{
context.BeginRequest+=new EventHandler(context_BeginRequest);
}
public void Dispose()
{
}
private void context_BeginRequest(object sender, EventArgs e)
{
try
{
Microsoft.ApplicationBlocks.ExceptionManagement.ExceptionManager.Publish( new Exception("Log attept") );
HttpApplication app = (HttpApplication)sender;
string method = app.Request.RawUrl;
SaveUseToDatabase( app.Request.UserHostAddress, method );
}
catch( Exception ex )
{
try
{
Microsoft.ApplicationBlocks.ExceptionManagement.ExceptionManager.Publish( ex );
}
catch{}
}
}
}
编译该 dll 后,我将其添加到 webservice 的 bin 文件夹中,并在 webservice 的 web.config 中添加:
<system.web>
<httpModules>
<add name="TrackingModule" type="WebserviceTrackingModule.TrackingModule, WebserviceTrackingModule"/>
这在我的计算机上工作正常,但是当我将其复制到生产服务器时,没有任何反应。 数据库中没有新条目,ExceptionManager 没有记录任何条目。 就好像它根本不存在一样。
我可以缺少什么吗?
编辑: 在执行另一次测试后,我可以补充说,当我将其添加到具有自己的顶级虚拟目录的 Web 服务时,它会起作用。 它不适用于驻留在作为另一个虚拟目录的子文件夹的虚拟目录中的 Web 服务。
我知道 HttpModules 设置由子目录继承,但看起来父目录的存在妨碍了。
I've created a simple HttpModule to log the uses of my existing webservice. There's a dll containing a single class
public class TrackingModule : System.Web.IHttpModule
{
public TrackingModule(){}
public void Init(System.Web.HttpApplication context)
{
context.BeginRequest+=new EventHandler(context_BeginRequest);
}
public void Dispose()
{
}
private void context_BeginRequest(object sender, EventArgs e)
{
try
{
Microsoft.ApplicationBlocks.ExceptionManagement.ExceptionManager.Publish( new Exception("Log attept") );
HttpApplication app = (HttpApplication)sender;
string method = app.Request.RawUrl;
SaveUseToDatabase( app.Request.UserHostAddress, method );
}
catch( Exception ex )
{
try
{
Microsoft.ApplicationBlocks.ExceptionManagement.ExceptionManager.Publish( ex );
}
catch{}
}
}
}
After compiling the dll I add it to webservice's bin folder and in webservice's web.config I add:
<system.web>
<httpModules>
<add name="TrackingModule" type="WebserviceTrackingModule.TrackingModule, WebserviceTrackingModule"/>
This works fine on my computer, but when I copy it to production server, nothing happens. No new entries in database, no entries logged by ExceptionManager. As if it's not there at all.
What can I be missing?
Edit:
After performing another test I can add that it works when I add it for a webservice that has it's own top-level virtual directory. It doesn't work for webservices that reside in virtual directories that are subfolders of another virtual directory.
I know that HttpModules settings are being inherited by subdirectories, but it looks like the existence of parent directory gets in the way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信我已经找到了更好的解决方案。 在运行时而不是在 Web 配置中附加模块。 请查看 Rick Strahl 的博客文章了解详细信息。
I believe I have found a better solution. Attach the module at runtime instead of in the web config. Check out Rick Strahl's blog post for the details.
好吧,我正在回答我自己的问题。
当您定义时它不起作用 在子目录的 web.config 中,即使子目录配置为应用程序也是如此。 到目前为止,我找到的唯一解决方案是在中定义它们。 根应用程序(父目录)的 web.config 中的标记。
我不喜欢它:(
OK, I'm answering my own question.
It doesn't work when you define <httpModules> in subdirectory's web.config, even when the subdirectory is configured as an application. The only solution I found so far is to define them within <location> tag in web.config of root application (parent directory).
I don't like it :(
我在 http://forums.iis.net/t/1151924 中找到了这个问题的答案。 ASPX
因此,翻译一下:
如果您遇到 httphandlers 问题,
请将您的处理程序添加到 system.webserver 中的模块节点中,看看是否有效
复制脚本模块使用的格式。
I found the answer to this question in http://forums.iis.net/t/1151924.aspx
So to translate that:
If you are having a problem with httphandlers
add your handler to the modules node in system.webserver and see if that works
Copy the format used for scriptmodule.
这有效吗?
每个请求都肯定会调用 context_BeginRequest 方法吗?
Does this work?
And is the context_BeginRequest method definitely being called for each request?