locked
HttpModule firing repeatedly on EndRequest for gif imagines RRS feed

  • Question

  • User1967172863 posted

    I'm running IIS 6 on Windows 2003.  I have a banner manager and lots of sites out there already use it to track impressions.  It was using an old ISAPI filter to log them, but since I've migrated to Windows 2003, I can't get that ISAPI filter to work anymore.  So, I'm trying to make it work with a HttpModule.  It actually works great in the Visual Studio integrated web server - in debug mode.

    However, when I deployed it to the server, I'm seeing an odd problem.  On each banner hit, it's logging something like 100 impressions.  This doesn't happen in the Visual Studio web server.  One difference is that I had to map .gif extension to the ASP ISAPI extension on Windows 2003, so that the HttpModule would get fired.

    Requests to my server look like the following: http://www.server.com/banners/foo.gif?refid=12345.  I need to track the impression and the ref id in the database.  I cannot easily change the URL above because thousands of sites are already using this format to display banners, so I need to figure out some way to make it work in Windows 2003\IIS 6 as is.  Any suggestions or ideas what might be wrong or the best approach to do this?

    I also apologize that I'm a bit of a newb on the .NET stuff.  I've been in Java land for some time and am learning the .NET side as I go.

    Thursday, August 2, 2007 12:31 PM

Answers

  • User-225114762 posted

    Put logging into the Init() and End_Request() methods, and include info about the request URL, the thread id etc, and then inspect the log. Or, if you can reproduce it in a development machine, just set a few breakpoints and see just what is happening.

    Windows 2003 and gif handlers are pretty basic. I still don't think it's an ASP.NET bug. I do know there are ASP.NET bugs, I've reported at least one myself, but in this case I'm sceptical. If you do see a difference in behavior between Windows 2000 and Windows 2003 with ASP.NET 2.0 and the same app in both cases, it's probably related to how IIS 6 and IIS 5 differs - because they do, pretty much actually.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 6, 2007 10:17 AM

All replies

  • User1622957740 posted

    Are you checking for your particular path or images in your manager? You've basically routed ALL gif images to ASP.NET now so EVERY GIF image will fire through your module. I suspect that's why you're seeing this massive traffic in your manager.

    You need to figure out a way to filter the requests to your module so you only deal with the ones that are infect supposed to go to the banner manager. That might mean limiting the ISAPI mapping to the specific virtual directory of your banner manager (ie. install it in a separate virtual where nothing else fires) or otherwise looking at the URL and figuring out based on the pattern whether its meant for you or not.

    Another thing to consider is using a Handler rather than a module - you can then filter the name a little more easily with the Http Handler entry in web.config.

    Hope this helps,

    +++ Rick ---

    Thursday, August 2, 2007 10:49 PM
  • User1967172863 posted

    The site that this is installed on actually only serves banners (no other pages or images at all actually).

     What I'm seeing is that when I hit a banner, it thinks it got hit a hundred times or so (I can guarantee that this banner with this id only got hit once, but I have a hundred entries in my log tables).  I also notice that it's VERY slow returning the banner, so it's obvious it's churning on something.

    I might try the handler approach, but I was under the assumption that I would still have to map an extension to ASP.NET even with the handler.  The extension would have to .gif, because I can't change the URL's on all the sites that are already using this.
     

    Thursday, August 2, 2007 10:58 PM
  • User1622957740 posted

    Yes you still will need the ISAPI mapping for GIF.

    I'm not sure why you would get more than one hit though per image though. IIS will only pass one request through per context hit and so each of the events in the pipeline only fire once. Could it be that you have concurrency issues in some way? Remember there can be multiple requests accessing the same data at the same time.

    A handler is probably a better fit for your scenario because you have a known entry point. that'll also reduce any parsing code you have to write to filter out requests.

    I still suspect that there are hits getting fired that you might not be expecting. Maybe you should log all URLs as they come in to a text file and see what you get - maybe a robot or SPAM bot is going wild on your site...

    +++ Rick ---

    Thursday, August 2, 2007 11:25 PM
  • User1967172863 posted

    Well, my fix is an odd one, but it worked... :)

    I used an HttpHandler just to serve up the gif.  It's not doing any database logging or anything, it's just serving it up.  Then, I have my HttpModule, which I didn't change at all, and it's actually logging impressions to the database.  So, this phenomena only occurs if the asp.net ISAPI is serving the gif directly.  When it is, it literally calls the event handler registered in the HttpModule 101 times.  I don't know what the magic number is with 101, but that's what it's doing.  I even duplicated this by simplifying the HttpModule and trying a different server.  For reference, heres my HttpModule code that's being used:

     

            public void Init(HttpApplication context)
            {
                context.EndRequest += new EventHandler(context_EndRequest);
            }
    
            public void context_EndRequest(object sender, System.EventArgs args)
            {
                try
                {
                    HttpApplication application = (HttpApplication)sender;
                    HttpRequest req = application.Request;
    
            ...
      

     The HttpHandler I'm using to just serve the gif is as follows:

      

        class ImpressionHttpHandler : IHttpHandler
        {
        	public void ProcessRequest(HttpContext context)
            {
                string FilePath = context.Request.FilePath;
                string FileName = context.Server.MapPath(FilePath);
    
                context.Response.ContentType = "image/gif";
                context.Response.WriteFile(FileName);
            }
    
     
    As you can see, it's a ridiculously simple HttpHandler.  But, if I remove it, then the HttpModule event gets called 101 times.  If it's there, it gets called once.  This doesn't make sense to me and honestly smells like an IIS or .Net bug to me.
     



     

    Friday, August 3, 2007 2:41 AM
  • User-225114762 posted

    I think there's more to the story that what you've showed us so far - probably something you consider to be irrelevant to the discussion. It's unlikely it's a bug, this is core functionality used by just about every site out there. It usually works just fine.

    The behavior you describe is often due to mistakenly registering the event handler delegate multiple times, perhaps via a static. The magic number 101 could be connected to the default number of concurrent requests for ASP.NET which I if I recall correctly is 100/CPU, so if you add the event handler to a static for every time a new HttpApplication instance is created... (Don't know where the extra '1' comes from, this is a pretty shaky theory based on very little info).

    Monday, August 6, 2007 3:32 AM
  • User1967172863 posted

    Yes, but, this only happens in one situation - on Windows 2003 when I register .gif to the ASP.NET ISAPI.  I cannot reproduce this when I hit an aspx page (the attached module only fires once) and I cannot reproduce it when EXACTLY the same application is installed on a Windows 2000 server.  Only Windows 2003 and only with gif registered to be handled by ASP.NET.  That's why I think it might be a bug.  It's actually quite isolated and I doubt there are that many people out that are registering images to the ASP.NET handler.  I've reproduced this on two Windows 2003 boxes.  One was pristine and was not getting hit by anybody else (it was inside my firewall).  After a single hit by me, the module was invoked a hundred times.  Also, it's always a hundred, it never is less or more.

    Monday, August 6, 2007 9:47 AM
  • User-225114762 posted

    Put logging into the Init() and End_Request() methods, and include info about the request URL, the thread id etc, and then inspect the log. Or, if you can reproduce it in a development machine, just set a few breakpoints and see just what is happening.

    Windows 2003 and gif handlers are pretty basic. I still don't think it's an ASP.NET bug. I do know there are ASP.NET bugs, I've reported at least one myself, but in this case I'm sceptical. If you do see a difference in behavior between Windows 2000 and Windows 2003 with ASP.NET 2.0 and the same app in both cases, it's probably related to how IIS 6 and IIS 5 differs - because they do, pretty much actually.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 6, 2007 10:17 AM