简体   繁体   English

HttpModule似乎不起作用

[英]HttpModule doesn't seem to work

I've created a simple HttpModule to log the uses of my existing webservice. 我创建了一个简单的HttpModule来记录我现有的webservice的用法。 There's a dll containing a single class 有一个包含单个类的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{}
        }
    }
}

After compiling the dll I add it to webservice's bin folder and in webservice's web.config I add: 编译完dll之后,我将它添加到webservice的bin文件夹中,并在webservice的web.config中添加:

<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. 数据库中没有新条目,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. 编辑:执行另一个测试后,我可以添加它,当我为具有自己的顶级虚拟目录的webservice添加它时它可以工作。 It doesn't work for webservices that reside in virtual directories that are subfolders of another virtual directory. 它不适用于驻留在作为另一个虚拟目录的子文件夹的虚拟目录中的Web服务。

I know that HttpModules settings are being inherited by subdirectories, but it looks like the existence of parent directory gets in the way. 我知道HttpModules设置正在被子目录继承,但看起来父目录的存在会妨碍它。

I believe I have found a better solution. 我相信我找到了更好的解决方案。 Attach the module at runtime instead of in the web config. 在运行时而不是在Web配置中附加模块。 Check out Rick Strahl's blog post for the details. 查看Rick Strahl的博客文章了解详情。

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. 在子目录的web.config中定义<httpModules>时,即使将子目录配置为应用程序,它也不起作用。 The only solution I found so far is to define them within <location> tag in web.config of root application (parent directory). 到目前为止,我找到的唯一解决方案是在根应用程序(父目录)的web.config中的<location>标记内定义它们。

I don't like it :( 我不喜欢它:(

I found the answer to this question in http://forums.iis.net/t/1151924.aspx 我在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: 在盯着3.5相关的web.config代码后,我意识到我的模块需要添加到新的部分:

<system.webserver> <system.webserver>

<modules> <模块>

instead of system.web...at least it's working now. 而不是system.web ...至少它现在正在工作。

So to translate that: 所以翻译一下:

If you are having a problem with httphandlers 如果您遇到httphandlers问题

add your handler to the modules node in system.webserver and see if that works Copy the format used for scriptmodule. 将您的处理程序添加到system.webserver中的modules节点,并查看是否有效复制用于scriptmodule的格式。

Does this work? 这有用吗?

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

And is the context_BeginRequest method definitely being called for each request? 是否肯定会为每个请求调用context_BeginRequest方法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM