IHttpModule Dispose

G

Guest

When is the IHttpModule Dispose driven ?....at application recycle time ?

I developed a test HttpModule to trace INIT, BeginRequest, EndRequest and
Dispose....and the Dispose never gets driven. After a while the INIT is
redriven however the Dispose was never driven.

I am trying to locate the best means to hook ASP.NET application recycling
...... Is IHttpModule Dispose a solution for this and if so how ?...... or
should I use IRegisterObject ?.... I have seen some web postings on
IRegisterObject.
 
G

Guest

Thanks much for the post.... however can you clarify when the IHttpModule
Dispose is driven ?....just curious
 
S

Samuel R. Neff

How exactly are you tracing Dispose() ? Is it possible that Dispose()
is being called but whatever resources it's using to trace were
already disposed.

Sam
 
G

Guest

Hi,
MSDN Documentation says following about IHTTPModule Dispose:
The Dispose method is called once—when the application terminates. You
should keep in mind that the application terminates and reinitializes not
only when the application or IIS restarts, but also when the Web.config
configuration file or a dependent assembly is modified in any way.
Although i have not tried but below article by EggHeadCafe shows how to hook
up IHTTPModule Dispose and how unhandled exceptions can be trapped through
IHTTPModule implementation.
http://www.eggheadcafe.com/articles/20060305.asp
Hope this helps.
 
G

Guest

Sam....thanks for your suggestion on the IHttpModule Dispose, however I am
99% certain it is NOT being driven. I have even tried "throwing an
exception" in the Dispose....and NOTHING. Yet, I can force an application
recycling by changing the web.config....and I will see a new INIT being
driven when I do this.

Code follows...

using System;
using System.Web;
using System.Web.Hosting;
using System.Collections;
using System.Text;
using System.IO;


public class HttpModuleTrace : IHttpModule
{
~HttpModuleTrace()
{
TraceIt("Finalizer");
}

public void Init(HttpApplication application)
{
TraceIt("Init");

application.BeginRequest += (new
EventHandler(this.Application_BeginRequest));
application.EndRequest += (new
EventHandler(this.Application_EndRequest));
application.Disposed += (new EventHandler(this.Application_Disposed));
}

public void Dispose()
{
TraceIt("Dispose");
}

private void Application_BeginRequest(Object source, EventArgs e)
{
TraceIt("Application_BeginRequest");

HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
context.Response.Write("<h1><font color=red>HelloWorldModule:
Beginning of Request</font></h1><hr>");
}

private void Application_EndRequest(Object source, EventArgs e)
{
TraceIt("Application_EndRequest");

HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
context.Response.Write("<hr><h1><font color=red>HelloWorldModule:
End of Request</font></h1>");
}

private void Application_Disposed(Object source, EventArgs e)
{
TraceIt("Application_Disposed");
}

private string GetTraceFilePath()
{
System.Web.HttpContext http = System.Web.HttpContext.Current;
return http.Request.PhysicalApplicationPath + "HttpModuleTrace.txt";
}

private void TraceIt(string TraceText)
{
using (StreamWriter sw = new StreamWriter(GetTraceFilePath(), true,
System.Text.Encoding.UTF8))
{
sw.WriteLine(DateTime.Now.ToString("d-MMM-yyyy HH:mm:ss.fff") +
" - " + TraceText);
sw.Flush();
sw.Close();
}
}
}
 
S

Samuel R. Neff

I would suspect problem is here:

private string GetTraceFilePath()
{
HttpContext http = Current;
return http.Request.PhysicalApplicationPath +
"HttpModuleTrace.txt";
}

When Dispose is called I doubt there is a current context so you're
probably getting a null reference exception which gets subsequently
ignored because the module is being disposed and ASP.NET doesn't know
what to do with it.

Try logging to a hard coded absolute path instead of using HttpContext
and see if you get the disposed message then.

HTH,

Sam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top