473,513 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HttpModule multithreading and request and response corelation

I have a scenario where I log a resquest to a database table and update the
request with a corresponding response including the response time. I am using
an HttpModule to do this.

My challenge is how to corelate the response to a corresponding request. I
am looking for a sure proof threadsafe way to corelate a response to a
coresponding request message.
Two things that concerns me:-

1. I am using the onBeginRequest event to capture the request and persist it
in the database and a filter class inheriting from the stream class to
capture the response and update a request row in the database with the
correspond response using an Id I generate to correct the messages.

2. My question is that if I create a guid in the onBeginRequest method and
pass it as a parameter to the filter class will this work from correlation
and multithreading standpoint since the messgae Id is a private memebr
variable in the filter class and is accessed by the overriden write method?

sample code

private void OnBeginRequest( object sender, EventArgs args )
{
string requestContent = string.Empty;

// HttpApplication httpApp = sender as HttpApplication;

// current request context
HttpContext context = HttpContext.Current;

// long position = httpApp.Context.Request.InputStream.Position;
long position = context.Request.InputStream.Position;

// Stream requestStream = httpApp.Context.Request.InputStream;

Stream requestStream = context.Request.InputStream;

byte[] buffer = new byte[ requestStream.Length ];

int read = requestStream.Read( buffer, 0, (int)requestStream.Length
);

//reset the stream to the original position
// httpApp.Request.InputStream.Position = position;

context.Request.InputStream.Position = position;

requestContent = System.Web.HttpUtility.HtmlDecode(
System.Web.HttpUtility.UrlDecode(System.Text.Encod ing.Default.GetString(
buffer ) ));

if( requestContent.Length == 0 ) return;

//generate message Id to corelate the request and resoponse
string messageId = System.Guid.NewGuid().ToString();

// For Get and Http posts remove the name from the name/value pair
requestContent = requestContent.Substring( requestContent.IndexOf( "=<" ) +
1 );

// append the messageId to the message
requestContent = requestContent.Insert( requestContent.LastIndexOf("/") - 1,
"<messageId>" + messageId + "</messageId>" );

LogRequestInfo( requestContent );

// apply the filter to access the response stream
// _responseStream = new ResponseStream( _httpApp.Response.Filter );

// httpApp.Context.Response.Filter = new ResponseStream(
HttpContext.Current.Response.Filter, messageId );
_responseStream = new ResponseStream( context.Response.Filter, messageId );
context.Response.Filter = _responseStream;

}
The critical question here is that there is very little documentation from
Microsoft on threading issues regarding use of httpmodules and how the
httpmodule instances are created and used.

sample filter class code

public sealed class ResponseStream : Stream
{
private Stream _responseStream;
private string _messageId;

public ResponseStream( Stream sink )
{
_responseStream = sink;
}

public ResponseStream( Stream sink, string messageId )
{
_responseStream = sink;
_messageId = messageId;
}

public override void Write( byte[] buffer, int offset, int count )
{
_responseStream.Write( buffer, offset, count );

// get the response string from the byte stream
string responseString = System.Web.HttpUtility.HtmlDecode(
System.Web.HttpUtility.UrlDecode(Encoding.Default. GetString( buffer ).Trim()
) );

//append the message Id to the response message
responseString = responseString.Insert( responseString.LastIndexOf("/") - 1,
"<messageId>" + _messageId + "</messageId>" );

// log the response
LogResponseInfo( responseString );
}
Thanks for your help in advance.
SOA Freak

Nov 19 '05 #1
7 4116
Hi Shapiro:

I'm a little confused by the question, so I apologize in advance if
this answer is not what you are looking for.

If you are trying to tie together a logical thread of execution among
various modules and filters, one place to keep state about the
response is in the Context.Items collection. This contents of the
collection are only around for the duration of a request response. You
could plop in an ID here that you get back when logging the request,
and retrieve it to know what record to update later on.

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Mon, 6 Dec 2004 16:57:02 -0800, "Shapiro"
<Sh*****@discussions.microsoft.com> wrote:
I have a scenario where I log a resquest to a database table and update the
request with a corresponding response including the response time. I am using
an HttpModule to do this.

My challenge is how to corelate the response to a corresponding request. I
am looking for a sure proof threadsafe way to corelate a response to a
coresponding request message.
Two things that concerns me:-

1. I am using the onBeginRequest event to capture the request and persist it
in the database and a filter class inheriting from the stream class to
capture the response and update a request row in the database with the
correspond response using an Id I generate to correct the messages.

2. My question is that if I create a guid in the onBeginRequest method and
pass it as a parameter to the filter class will this work from correlation
and multithreading standpoint since the messgae Id is a private memebr
variable in the filter class and is accessed by the overriden write method?

sample code

private void OnBeginRequest( object sender, EventArgs args )
{
string requestContent = string.Empty;

// HttpApplication httpApp = sender as HttpApplication;

// current request context
HttpContext context = HttpContext.Current;

// long position = httpApp.Context.Request.InputStream.Position;
long position = context.Request.InputStream.Position;

// Stream requestStream = httpApp.Context.Request.InputStream;

Stream requestStream = context.Request.InputStream;

byte[] buffer = new byte[ requestStream.Length ];

int read = requestStream.Read( buffer, 0, (int)requestStream.Length
);

//reset the stream to the original position
// httpApp.Request.InputStream.Position = position;

context.Request.InputStream.Position = position;

requestContent = System.Web.HttpUtility.HtmlDecode(
System.Web.HttpUtility.UrlDecode(System.Text.Enco ding.Default.GetString(
buffer ) ));

if( requestContent.Length == 0 ) return;

//generate message Id to corelate the request and resoponse
string messageId = System.Guid.NewGuid().ToString();

// For Get and Http posts remove the name from the name/value pair
requestContent = requestContent.Substring( requestContent.IndexOf( "=<" ) +
1 );

// append the messageId to the message
requestContent = requestContent.Insert( requestContent.LastIndexOf("/") - 1,
"<messageId>" + messageId + "</messageId>" );

LogRequestInfo( requestContent );

// apply the filter to access the response stream
// _responseStream = new ResponseStream( _httpApp.Response.Filter );

// httpApp.Context.Response.Filter = new ResponseStream(
HttpContext.Current.Response.Filter, messageId );
_responseStream = new ResponseStream( context.Response.Filter, messageId );
context.Response.Filter = _responseStream;

}
The critical question here is that there is very little documentation from
Microsoft on threading issues regarding use of httpmodules and how the
httpmodule instances are created and used.

sample filter class code

public sealed class ResponseStream : Stream
{
private Stream _responseStream;
private string _messageId;

public ResponseStream( Stream sink )
{
_responseStream = sink;
}

public ResponseStream( Stream sink, string messageId )
{
_responseStream = sink;
_messageId = messageId;
}

public override void Write( byte[] buffer, int offset, int count )
{
_responseStream.Write( buffer, offset, count );

// get the response string from the byte stream
string responseString = System.Web.HttpUtility.HtmlDecode(
System.Web.HttpUtility.UrlDecode(Encoding.Default .GetString( buffer ).Trim()
) );

//append the message Id to the response message
responseString = responseString.Insert( responseString.LastIndexOf("/") - 1,
"<messageId>" + _messageId + "</messageId>" );

// log the response
LogResponseInfo( responseString );
}
Thanks for your help in advance.
SOA Freak


Nov 19 '05 #2
Scott,

The Context.Items collection is not available in the filter class. I am
overridding the write method. My sample code is showing what I am trying to
achive.
Regards,
Shepard

"Scott Allen" wrote:
Hi Shapiro:

I'm a little confused by the question, so I apologize in advance if
this answer is not what you are looking for.

If you are trying to tie together a logical thread of execution among
various modules and filters, one place to keep state about the
response is in the Context.Items collection. This contents of the
collection are only around for the duration of a request response. You
could plop in an ID here that you get back when logging the request,
and retrieve it to know what record to update later on.

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Mon, 6 Dec 2004 16:57:02 -0800, "Shapiro"
<Sh*****@discussions.microsoft.com> wrote:
I have a scenario where I log a resquest to a database table and update the
request with a corresponding response including the response time. I am using
an HttpModule to do this.

My challenge is how to corelate the response to a corresponding request. I
am looking for a sure proof threadsafe way to corelate a response to a
coresponding request message.
Two things that concerns me:-

1. I am using the onBeginRequest event to capture the request and persist it
in the database and a filter class inheriting from the stream class to
capture the response and update a request row in the database with the
correspond response using an Id I generate to correct the messages.

2. My question is that if I create a guid in the onBeginRequest method and
pass it as a parameter to the filter class will this work from correlation
and multithreading standpoint since the messgae Id is a private memebr
variable in the filter class and is accessed by the overriden write method?

sample code

private void OnBeginRequest( object sender, EventArgs args )
{
string requestContent = string.Empty;

// HttpApplication httpApp = sender as HttpApplication;

// current request context
HttpContext context = HttpContext.Current;

// long position = httpApp.Context.Request.InputStream.Position;
long position = context.Request.InputStream.Position;

// Stream requestStream = httpApp.Context.Request.InputStream;

Stream requestStream = context.Request.InputStream;

byte[] buffer = new byte[ requestStream.Length ];

int read = requestStream.Read( buffer, 0, (int)requestStream.Length
);

//reset the stream to the original position
// httpApp.Request.InputStream.Position = position;

context.Request.InputStream.Position = position;

requestContent = System.Web.HttpUtility.HtmlDecode(
System.Web.HttpUtility.UrlDecode(System.Text.Enco ding.Default.GetString(
buffer ) ));

if( requestContent.Length == 0 ) return;

//generate message Id to corelate the request and resoponse
string messageId = System.Guid.NewGuid().ToString();

// For Get and Http posts remove the name from the name/value pair
requestContent = requestContent.Substring( requestContent.IndexOf( "=<" ) +
1 );

// append the messageId to the message
requestContent = requestContent.Insert( requestContent.LastIndexOf("/") - 1,
"<messageId>" + messageId + "</messageId>" );

LogRequestInfo( requestContent );

// apply the filter to access the response stream
// _responseStream = new ResponseStream( _httpApp.Response.Filter );

// httpApp.Context.Response.Filter = new ResponseStream(
HttpContext.Current.Response.Filter, messageId );
_responseStream = new ResponseStream( context.Response.Filter, messageId );
context.Response.Filter = _responseStream;

}
The critical question here is that there is very little documentation from
Microsoft on threading issues regarding use of httpmodules and how the
httpmodule instances are created and used.

sample filter class code

public sealed class ResponseStream : Stream
{
private Stream _responseStream;
private string _messageId;

public ResponseStream( Stream sink )
{
_responseStream = sink;
}

public ResponseStream( Stream sink, string messageId )
{
_responseStream = sink;
_messageId = messageId;
}

public override void Write( byte[] buffer, int offset, int count )
{
_responseStream.Write( buffer, offset, count );

// get the response string from the byte stream
string responseString = System.Web.HttpUtility.HtmlDecode(
System.Web.HttpUtility.UrlDecode(Encoding.Default .GetString( buffer ).Trim()
) );

//append the message Id to the response message
responseString = responseString.Insert( responseString.LastIndexOf("/") - 1,
"<messageId>" + _messageId + "</messageId>" );

// log the response
LogResponseInfo( responseString );
}
Thanks for your help in advance.
SOA Freak


Nov 19 '05 #3
Have you tried using HttpContext.Current to retrieve the current
context and get to the Item collection from inside the filter class?
There has to be a context, or you would not be able to respond to the
request.

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Tue, 7 Dec 2004 10:39:04 -0800, "Shapiro"
<Sh*****@discussions.microsoft.com> wrote:
Scott,

The Context.Items collection is not available in the filter class. I am
overridding the write method. My sample code is showing what I am trying to
achive.
Regards,
Shepard


Nov 19 '05 #4
Thats one route. I guess my questions are that how the httpmodules are
instantiated and handled in multithreading and wether what I an doing in the
code is correct. I have provided sample code for review.
"Scott Allen" wrote:
Have you tried using HttpContext.Current to retrieve the current
context and get to the Item collection from inside the filter class?
There has to be a context, or you would not be able to respond to the
request.

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Tue, 7 Dec 2004 10:39:04 -0800, "Shapiro"
<Sh*****@discussions.microsoft.com> wrote:
Scott,

The Context.Items collection is not available in the filter class. I am
overridding the write method. My sample code is showing what I am trying to
achive.
Regards,
Shepard


Nov 19 '05 #5
Each instance of the HttpApplication class will create the HttpModules
needed (and registered in web.config).

HttpApplication objects (despite what the name implies) are pooled by
the ASP.NET runtime, so there is more than one. For each incoming
request an instance of HttpApplication is taken from the free pool and
assigned to service the request.

This means an HttpModule will service only one request at a time, but
it's still dangerous to keep state around in the class, because they
are pooled and reused across requests.

I agree, it's not well doc'ed. This knownledge is based on
experimentation and the Reflector tool.

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Tue, 7 Dec 2004 11:47:09 -0800, "Shapiro"
<Sh*****@discussions.microsoft.com> wrote:
Thats one route. I guess my questions are that how the httpmodules are
instantiated and handled in multithreading and wether what I an doing in the
code is correct. I have provided sample code for review.
"Scott Allen" wrote:
Have you tried using HttpContext.Current to retrieve the current
context and get to the Item collection from inside the filter class?
There has to be a context, or you would not be able to respond to the
request.

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Tue, 7 Dec 2004 10:39:04 -0800, "Shapiro"
<Sh*****@discussions.microsoft.com> wrote:
>Scott,
>
>The Context.Items collection is not available in the filter class. I am
>overridding the write method. My sample code is showing what I am trying to
>achive.
>
>
>Regards,
>Shepard
>



Nov 19 '05 #6
Scott,

Thanks for the reply but you have not answered my question. Looking at the
sample code and the description of the issue I have at hand will that work?

Regards,
Shapiro
"Shapiro" wrote:
Thats one route. I guess my questions are that how the httpmodules are
instantiated and handled in multithreading and wether what I an doing in the
code is correct. I have provided sample code for review.
"Scott Allen" wrote:
Have you tried using HttpContext.Current to retrieve the current
context and get to the Item collection from inside the filter class?
There has to be a context, or you would not be able to respond to the
request.

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Tue, 7 Dec 2004 10:39:04 -0800, "Shapiro"
<Sh*****@discussions.microsoft.com> wrote:
Scott,

The Context.Items collection is not available in the filter class. I am
overridding the write method. My sample code is showing what I am trying to
achive.
Regards,
Shepard


Nov 19 '05 #7
I guess I'm not sure what exactly the question is, my apologies.
The code seems ok, but only testing will tell :)

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

On Tue, 7 Dec 2004 15:37:02 -0800, "Shapiro"
<Sh*****@discussions.microsoft.com> wrote:
Scott,

Thanks for the reply but you have not answered my question. Looking at the
sample code and the description of the issue I have at hand will that work?

Regards,
Shapiro
"Shapiro" wrote:
Thats one route. I guess my questions are that how the httpmodules are
instantiated and handled in multithreading and wether what I an doing in the
code is correct. I have provided sample code for review.
"Scott Allen" wrote:
> Have you tried using HttpContext.Current to retrieve the current
> context and get to the Item collection from inside the filter class?
> There has to be a context, or you would not be able to respond to the
> request.
>
> --
> Scott
> http://www.OdeToCode.com/blogs/scott/
>
>
> On Tue, 7 Dec 2004 10:39:04 -0800, "Shapiro"
> <Sh*****@discussions.microsoft.com> wrote:
>
> >Scott,
> >
> >The Context.Items collection is not available in the filter class. I am
> >overridding the write method. My sample code is showing what I am trying to
> >achive.
> >
> >
> >Regards,
> >Shepard
> >
>
>


Nov 19 '05 #8

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

Similar topics

3
2189
by: Samuel | last post by:
I wrote a very simple httpmodule and tried to compile it with no success. This is my code: ============== Imports System Imports System.Web Imports Microsoft.VisualBasic NameSpace...
16
8470
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
2
3423
by: Rob Mayo | last post by:
What I'm trying to do is Create an ASP.Net app that has both Windows-authenticated users and Anonymous users. The idea is this: When authenticated users attempt to access the site, their...
10
1871
by: Girish | last post by:
Ok, Ive been thinking about this problem for a while. I have 30 odd aspx pages (already built) on my website that I need to have some validation occur before the page should load. The validation...
0
1910
by: Vitali Dzz via .NET 247 | last post by:
Hello I need some help I need to write a code that analyze size of the uploaded file. I wrote such HttpModule: public void BeginRequest(object sender, EventArgs args) { // Create an...
2
4569
by: walter | last post by:
Hi there, I know there is pool of HttpApplications, and for each request coming in, HttpRuntime will dedicate one from pool to serve the request. My questions are : 1. since HttpModule is plug...
1
1884
by: Faraz | last post by:
Hi everyone, I am running into a slight problem. My understanding is that a custom HttpModule will run for every request made to the server, regardless of the extension. I do not experience this...
3
2234
by: Dan Sikorsky | last post by:
I use an HttpModule that handles unhandled exceptions from an .aspx.cs page by logging to a text file, logging to the event viewer's app log, and sending out an email to the website developer and...
0
1382
by: Andy | last post by:
Hi folks, I wrote a HttpModule to handle file uploads (on beginRequest) prior to the actual web form displaying the result. The file upload processing generally works very well also for large...
0
7175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7391
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7553
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7120
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5697
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4754
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3247
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1609
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.