tags:

views:

1306

answers:

3

According this MSDN article HttpApplication.EndRequest can be used to close or dispose of resources. However this event is not fired/called in my application.

We are attaching the handler in Page_Load the following way:

HttpContext.Current.ApplicationInstance.EndRequest += ApplicationInstance_EndRequest;

The only way is to use the Application_EndRequest handler in Global.asax, but this is not acceptable for us.

+1  A: 

The page is probably being disposed before the event fires. You might want to try to do your work in the Page_Unload handler.

Will
We need it on the level of Request. We already have workaround for this, but im interessed in fact it is not working the way it should according to MSDN documentation which is saying:The EndRequest event is always raised when the CompleteRequest method is called.
Crank
+2  A: 

Per the MSDN documentation, this event occurs AFTER the page is completed, just like BeginRequest. Therefore as far as I know it is not possible to catch this at the page level

Mitchel Sellers
A thought it can be tricky for debugger to catch this event. But I've added some code which wasn't called (I'm sure).
Crank
+6  A: 

You can use your own HttpModule to capture the EndRequest if you don't want to use the global.asax.

public class CustomModule : IHttpModule 
{
    public void Init(HttpApplication context)
    {
        context.EndRequest += new EventHandler(context_EndRequest);
    }

    private void context_EndRequest(object sender, EventArgs e)
    {
        HttpContext context = ((HttpApplication)sender).Context;
        // use your contect here
    }
}

You need to add the module to your web.config

<httpModules>
    <add name="CustomModule" type="CustomModule"/>
</httpModules>
Eduardo Campañó
Why is this the case? Why doesn't the event fire without being in an httpmodule?
mcintyre321
Read Mitchel Sellers answer, that's why. The simple way would be to use the global.asax, but if you cannot use it, you can subscribe your own module to catch the request.
Eduardo Campañó