HttpModule multithreading and request and response corelation

G

Guest

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.Encoding.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
 
S

Scott Allen

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/


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.Encoding.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
 
G

Guest

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 said:
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/


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.Encoding.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
 
S

Scott Allen

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.
 
G

Guest

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.
 
S

Scott Allen

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.
 
G

Guest

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
 
S

Scott Allen

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

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top