Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async HttpWebRequest with no wait from within a web application

Tags:

In my web application (ASP.NET) I have a block of code that uses HttpWebRequest to make a call to a REST service and continue execution. Right now it's taking longer than I would like to complete the full web request. The thing is that what the REST service returns isn't useful. Ideally I would like to send an Async web request to the REST service and then NOT wait for a response. The problem is that I've tried it out using

request.BeginGetResponse(New AsyncCallback(AddressOf myFunc), Nothing) 

To start an async request and instead of NOT waiting (which I would assume would be the default behavior of an async request) it continuously executes the callback function before executing the next line of code after BeginGetResponse.

I'm suspecting that ASP.NET may convert it to a sync request when it's within a web application. I'm led to believe this because there's a IAsyncResult result object that is passed into the callback function and when I examine its CompletedSynchronously property it's always set to true.

Does anyone know if it's possible to do an async HttpWebRequest (with no wait) from within an ASP.NET web application or is it always converted to a synchronous request?

like image 684
Adam Avatar asked Feb 01 '10 16:02

Adam


2 Answers

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(myUrl); //set up web request... ThreadPool.QueueUserWorkItem(o=>{ myRequest.GetResponse(); }); 

Also known as Fire-and-Forget.

like image 52
Bryan Batchelder Avatar answered Sep 16 '22 20:09

Bryan Batchelder


you are probably looking at "fire and forget" pattern. Here are some links.

http://weblogs.asp.net/albertpascual/archive/2009/05/14/fire-and-forget-class-for-asp-net.aspx

http://haacked.com/archive/2009/01/09/asynchronous-fire-and-forget-with-lambdas.aspx

http://www.eggheadcafe.com/articles/20060727.asp

hope this helps

like image 42
ram Avatar answered Sep 17 '22 20:09

ram