468,272 Members | 2,083 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,272 developers. It's quick & easy.

Asynchronous HTTPModule executes synchronously ?

Hi,

I need to write asynchronous HTTPModule which will sometimes execute long
job. I've written it using AddOnBeginRequestAsync but it still executes
synchronously - I am checking performance counters but after running X
requests which executes this "asynchronous" module all other requests go to
application queue - so it executes synchronously.

Code is based on the following article
http://msdn.microsoft.com/msdnmag/is...WickedCode/#S3

This is my sample code

public class RequestDelayModule : BaseHTTPModule {

protected override void Init() {
base.Init();
this.Application.AddOnBeginRequestAsync(new
BeginEventHandler(BeginRequest_AsyncBegin), new
EndEventHandler(BeginRequest_AsyncEnd));
return;
}

delegate void ABR(HttpContext Context);

IAsyncResult BeginRequest_AsyncBegin(object sender, EventArgs e,
AsyncCallback cb, object state) {
return new ABR(Application_BeginRequest).BeginInvoke(null, cb,
this.Context);
}

void BeginRequest_AsyncEnd(IAsyncResult ar) {
return;
}

void Application_BeginRequest(HttpContext Context) {
Thread.Sleep(5 * 60 * 1000);
}

}

What's wrong on this code that AddOnBeginRequestAsync uses threads from same
thread pool as is used for serving other ASP.NET requests ?
All suggestions how to write HTTPModule asynchronously are welcome :-).

Many thanks.
Oct 27 '07 #1
3 4446
I've resolved it by not invoking via delegate but calling thread not from
worker thread pool. But I had to implement IAsyncResult to do it - is there
other easier way how to call some method asynchronously (and not using worker
process thread pool) without implementing IAsyncResult ?

Thank you.

"John" wrote:
Hi,

I need to write asynchronous HTTPModule which will sometimes execute long
job. I've written it using AddOnBeginRequestAsync but it still executes
synchronously - I am checking performance counters but after running X
requests which executes this "asynchronous" module all other requests go to
application queue - so it executes synchronously.

Code is based on the following article
http://msdn.microsoft.com/msdnmag/is...WickedCode/#S3

This is my sample code

public class RequestDelayModule : BaseHTTPModule {

protected override void Init() {
base.Init();
this.Application.AddOnBeginRequestAsync(new
BeginEventHandler(BeginRequest_AsyncBegin), new
EndEventHandler(BeginRequest_AsyncEnd));
return;
}

delegate void ABR(HttpContext Context);

IAsyncResult BeginRequest_AsyncBegin(object sender, EventArgs e,
AsyncCallback cb, object state) {
return new ABR(Application_BeginRequest).BeginInvoke(null, cb,
this.Context);
}

void BeginRequest_AsyncEnd(IAsyncResult ar) {
return;
}

void Application_BeginRequest(HttpContext Context) {
Thread.Sleep(5 * 60 * 1000);
}

}

What's wrong on this code that AddOnBeginRequestAsync uses threads from same
thread pool as is used for serving other ASP.NET requests ?
All suggestions how to write HTTPModule asynchronously are welcome :-).

Many thanks.
Oct 27 '07 #2
Hi John,

Yes, as you've found .NET Asynchornous method invoking through
Delegate.BeginInvoke also pickup thread from .NET CLR threadpool. So for
ASP.NET Async HttpHandler or Async page, you should not use it. Actually,
there does exist some asynchronous execution(such as the webservice
webrequest's async operation) which doesn't consume CLR threadpool. Here is
a good web article which has mentioned this:

#Async Pages in ASP.NET 2.0 - some results
http://www.pluralsight.com/blogs/fri...0/19/2892.aspx

Anyway, I suggest you always test the result when you try directly use the
existing async execution operations in ASP.NET async page to ensure that
does help on threadpool threads(If you do not implement your own async
pattern).

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.



--------------------
>From: =?Utf-8?B?Sm9obg==?= <aa****@nospam.nospam>
References: <77**********************************@microsoft.co m>
Subject: RE: Asynchronous HTTPModule executes synchronously ?
Date: Sat, 27 Oct 2007 13:11:02 -0700
>
I've resolved it by not invoking via delegate but calling thread not from
worker thread pool. But I had to implement IAsyncResult to do it - is
there
>other easier way how to call some method asynchronously (and not using
worker
>process thread pool) without implementing IAsyncResult ?

Thank you.

"John" wrote:
>Hi,

I need to write asynchronous HTTPModule which will sometimes execute
long
>job. I've written it using AddOnBeginRequestAsync but it still executes
synchronously - I am checking performance counters but after running X
requests which executes this "asynchronous" module all other requests go
to
>application queue - so it executes synchronously.

Code is based on the following article
http://msdn.microsoft.com/msdnmag/is...WickedCode/#S3

This is my sample code

public class RequestDelayModule : BaseHTTPModule {

protected override void Init() {
base.Init();
this.Application.AddOnBeginRequestAsync(new
BeginEventHandler(BeginRequest_AsyncBegin), new
EndEventHandler(BeginRequest_AsyncEnd));
return;
}

delegate void ABR(HttpContext Context);

IAsyncResult BeginRequest_AsyncBegin(object sender, EventArgs e,
AsyncCallback cb, object state) {
return new ABR(Application_BeginRequest).BeginInvoke(null, cb,
this.Context);
}

void BeginRequest_AsyncEnd(IAsyncResult ar) {
return;
}

void Application_BeginRequest(HttpContext Context) {
Thread.Sleep(5 * 60 * 1000);
}

}

What's wrong on this code that AddOnBeginRequestAsync uses threads from
same
>thread pool as is used for serving other ASP.NET requests ?
All suggestions how to write HTTPModule asynchronously are welcome :-).

Many thanks.
Oct 29 '07 #3
Hi John,

Have you got any further ideas on this? If there is any other questions on
this ,please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
>From: st*****@online.microsoft.com (Steven Cheng[MSFT])
Organization: Microsoft
Date: Mon, 29 Oct 2007 02:40:54 GMT
Subject: RE: Asynchronous HTTPModule executes synchronously ?
>>
Hi John,

Yes, as you've found .NET Asynchornous method invoking through
Delegate.BeginInvoke also pickup thread from .NET CLR threadpool. So for
ASP.NET Async HttpHandler or Async page, you should not use it. Actually,
there does exist some asynchronous execution(such as the webservice
webrequest's async operation) which doesn't consume CLR threadpool. Here
is
>a good web article which has mentioned this:

#Async Pages in ASP.NET 2.0 - some results
http://www.pluralsight.com/blogs/fri...0/19/2892.aspx

Anyway, I suggest you always test the result when you try directly use the
existing async execution operations in ASP.NET async page to ensure that
does help on threadpool threads(If you do not implement your own async
pattern).

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.


Nov 1 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Ollie Riches | last post: by
2 posts views Thread by Darryl A. J. Staflund | last post: by
4 posts views Thread by Michael C | last post: by
3 posts views Thread by Thomas Nielsen | last post: by
10 posts views Thread by Susan | last post: by
reply views Thread by =?Utf-8?B?aWFtc2Rr?= | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.