Multi-threaded HTTP Module

G

GWiz

All,

I am trying to write a multi-threaded HTTP module that handles the
OnBeginRequest and logs it. What I am trying to do is to offload the
logging to another thread which will allow the http module to release
the OnBeginRequest.

The issue that I have is that I keep on getting "Request is not
available in this context" exceptions.

The following is the code I am using:

using System;
using System.Web;

public class ProcessRequest
{
public HttpApplication HttpRequest;

public void ProcessingThread()
{
try
{
string sRequestUrl;
if (HttpRequest.Request!=null)
{
sRequestUrl=HttpRequest.Request.HttpMethod;
System.Diagnostics.Debug.WriteLine(sRequestUrl);
}
}
catch (System.Exception ex)
{
throw ex;
}
}
}


public class HttpRequestLogger : IHttpModule
{
protected System.Collections.Queue oHttpRequestQueue= new
System.Collections.Queue();
protected bool bModuleRunning =false;


public HttpRequestLogger()
{
}

// In the Init function, register for HttpApplication
// events by adding your handlers.
public void Init(HttpApplication application)
{
application.BeginRequest +=
(new EventHandler(this.Application_BeginRequest));
application.EndRequest +=
(new EventHandler(this.Application_EndRequest));
}

private void Application_BeginRequest(Object source, EventArgs e)
{
// Create HttpApplication and HttpContext objects to access
// request and response properties.
System.Threading.Thread oProcessingThread;
HttpApplication application = (HttpApplication)source;
ProcessRequest oRequestProcessor=new ProcessRequest();
oRequestProcessor.HttpRequest=application;
oProcessingThread =new System.Threading.Thread(new
System.Threading.ThreadStart(oRequestProcessor.ProcessingThread));
oProcessingThread.Start();
}

private void Application_EndRequest(Object source, EventArgs e)
{
// HttpApplication application = (HttpApplication)source;
// HttpContext context = application.Context;
}

public void Dispose()
{
}
}
 
S

Scott Allen

Hi GWiz:

It's possible that between the time you set the HttpRequest property
(oRequestProcessor.HttpRequest=application;) and the time the thread
actually runs, that the original request will run to completion. It
could be that the ASP.NET runtime has cleared out all the data
structures for the request you are trying to log.

My suggestion would be to start simple and not use a second thread.
Even if the logging is a bottleneck, creating a new thread for each
request is certainly going to be more overhead and you might bog down
the server even more.
 
J

John Timney \( MVP \)

Your not passing the context to the thread, and probably cant as Scott
suggest, because of the duration of the request not remaining in scope. You
would probably have to take the values from the request and pass those to
the thread, rather than relying on context.

Have a read of this excellent article for some ideas:

http://www.west-wind.com/presentations/howaspnetworks/howaspnetworks.asp

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director
 

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,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top