user34402
user34402

Reputation: 147

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.

Upvotes: 3

Views: 5442

Answers (4)

regex
regex

Reputation: 3601

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.

Upvotes: 3

Stephen Lacy
Stephen Lacy

Reputation:

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.

Upvotes: 1

user34402
user34402

Reputation: 147

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

Upvotes: 1

Bullines
Bullines

Reputation: 5696

Does this work?

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

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

Upvotes: 0

Related Questions