Problem with hooking into FileHandler

Topics: ASP.NET 2.0, Business Logic Layer
Feb 4, 2008 at 10:19 PM
I am finishing up an extension that deals with file downloading. Everything works file except that BlogEngine ends up running my extension twice when a file is downloaded… Perhaps I hooked into the wrong event?

public NewExtension()
{
FileHandler.Serving += new EventHandler<EventArgs> (NewExtension);
}

private void NewExtension (object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
if (!string.IsNullOrEmpty(context.Request.QueryString"file"))
{
// Do something here…
}
}

So when walking through this in the debugger I see that the extension has been called twice. I also noticed that in the Filehandler.cs file

public void ProcessRequest(HttpContext context)

is called twice also… Is this supposed to happen twice?
Coordinator
Feb 5, 2008 at 3:47 AM
Looks like you are calling a constructor in your event handler, this is why it runs twice. It should look more like:

public NewExtension()
{
FileHandler.Serving += new EventHandler<EventArgs> (FileHandler_Serving);
}

private void FileHandler_Serving(object sender, EventArgs e){ ... }

Take a look at example here: http://rtur.net/blog/post/2008/01/Download-Counter-Extension.aspx

Feb 5, 2008 at 6:58 AM
Thanks for that. I didn't really see a difference from my code and your response but your extension really cleared things up. As it turns out I was creating a similar extension to what you did. I did, however, make some differences in how clicks are recorded and displayed. It would be great if you could have a glance to make sure I didn't muck it up...

http://www.dscoduc.com/post/2008/02/Track-file-downloads---BlogEngine-Extension-and-Control.aspx

Thanks again!

btw, Al also really helped me out on this...