Asynchronous HTTPModule executes synchronously ?

G

Guest

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/issues/07/03/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.
 
G

Guest

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

Steven Cheng[MSFT]

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/fritz/archive/2004/10/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.







--------------------
 
S

Steven Cheng[MSFT]

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: (e-mail address removed) (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/fritz/archive/2004/10/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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top