简体   繁体   中英

HttpModule doesn't seem to work

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.

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.

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 :(

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.

Does this work?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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