HttpModule 似乎不起作用

发布于 2024-07-11 07:40:39 字数 1511 浏览 4 评论 0原文

我创建了一个简单的 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 技术交流群。

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

发布评论

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

评论(4

漫雪独思 2024-07-18 07:40:39

我相信我已经找到了更好的解决方案。 在运行时而不是在 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.

你在我安 2024-07-18 07:40:39

好吧,我正在回答我自己的问题。

当您定义时它不起作用 在子目录的 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 :(

別甾虛僞 2024-07-18 07:40:39

我在 http://forums.iis.net/t/1151924 中找到了这个问题的答案。 ASPX

哦,好吧,有一点
消除过程永远不会失败
我。

盯着3.5相关后
web.config 代码,我意识到我的
需要将模块添加到新的
部分:

<系统.webserver>

   <模块>

而不是 system.web...至少它是
现在正在工作。

因此,翻译一下:

如果您遇到 httphandlers 问题,

请将您的处理程序添加到 system.webserver 中的模块节点中,看看是否有效
复制脚本模块使用的格式。

I found the answer to this question in http://forums.iis.net/t/1151924.aspx

oh well, a little
process-of-elimination never fails
me.

After staring at the 3.5-related
web.config code, I realized that my
module needed to be added to the new
section:

<system.webserver>

    <modules>

instead of system.web...at least it's
working now.

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.

小梨窩很甜 2024-07-18 07:40:39

这有效吗?

<add name="TrackingModule" type="WebserviceTrackingModule.TrackingModule" />

每个请求都肯定会调用 context_BeginRequest 方法吗?

Does this work?

<add name="TrackingModule" type="WebserviceTrackingModule.TrackingModule" />

And is the context_BeginRequest method definitely being called for each request?

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