ASP.NET HttpApplication.EndRequest event not fired

.net, asp.net

Solution

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>

Problem

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.

Original source