473,382 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Multi-threaded HTTP Module

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.Pro cessingThread));
oProcessingThread.Start();
}

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

public void Dispose()
{
}
}

Nov 19 '05 #1
2 1980
On 15 Oct 2005 21:59:00 -0700, "GWiz" <ji*********@gmail.com> wrote:

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.

--
Scott
http://www.OdeToCode.com/blogs/scott/

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.Pr ocessingThread));
oProcessingThread.Start();
}

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

public void Dispose()
{
}
}


Nov 19 '05 #2
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/presentatio...spnetworks.asp

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"Scott Allen" <sc***@nospam.odetocode.com> wrote in message
news:2q********************************@4ax.com...
On 15 Oct 2005 21:59:00 -0700, "GWiz" <ji*********@gmail.com> wrote:

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.

--
Scott
http://www.OdeToCode.com/blogs/scott/

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.P rocessingThread));
oProcessingThread.Start();
}

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

public void Dispose()
{
}
}

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours,...
0
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
27
by: DraguVaso | last post by:
Hi, The Multi column comboBox is a well spoken control, that a lot of people desire, and a lot of people buildtheir own. I tested a lot of them, but none really was what i wanted to have. But...
6
by: Joe | last post by:
I have 2 multi-list boxes, 1 displays course categories based on a table called CATEGORIES. This table has 2 fields CATEGORY_ID, CATEGORY_NAME The other multi-list box displays courses based on...
2
by: google | last post by:
Hello everyone, I am having an issue using the "Multi Select" option in a list box in MS Access 2003. I am making a form that users can fill out to add an issue to the database. Each issue can...
4
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
5
by: bobwansink | last post by:
Hi, I'm relatively new to programming and I would like to create a C++ multi user program. It's for a project for school. This means I will have to write a paper about the theory too. Does anyone...
23
by: Kaz Kylheku | last post by:
I've been reading the recent cross-posted flamewar, and read Guido's article where he posits that embedding multi-line lambdas in expressions is an unsolvable puzzle. So for the last 15 minutes...
1
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier...
11
by: woodey2002 | last post by:
This problem is driving me crazy. Hello there, i am trying to create a search form for records in my access database. The search form will contain text boxes and a multi select list box. The user...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.