I need process concurent requests at my application. I use this code:
public class AsyncHttpHelper
{
public static IEnumerable<XDocument> GetPagesAsXDocuments(IEnumerable<string> uris)
{
IEnumerable<IAsyncResult> asyncResults = uris
.Select(uri => (HttpWebRequest)WebRequest.Create(uri))
.Select(webRequest => webRequest.BeginGetResponse(null, webRequest));
WaitHandle[] handles = asyncResults.Select(asyncResult => asyncResult.AsyncWaitHandle).ToArray();
WaitHandle.WaitAll(handles);
var result = asyncResults
.Select(asyncResult =>
{
var httpWebRequest = (HttpWebRequest)asyncResult.AsyncState;
HttpWebResponse response;
try
{
response = (HttpWebResponse)httpWebRequest.EndGetResponse(asyncResult);
}
catch (Exception ex)
{
return null;
}
Stream responseStream = response.GetResponseStream();
if (responseStream == null)
{
return null;
}
using (var streamReader = new StreamReader(responseStream))
{
return XDocument.Load(streamReader);
}
});
return result;
}
}
But it correctly works only with 1 url for request. When I try get 2 or more urls my method hangs up. When I pause debugger shows me executing WaitHandle.WaitAll(handles); this line. So I see that not all async requests were done.
So what’s the problem. Why I can’t do several requests async
Thanks Andrew
I do not remember off the top of my head, but there was a setting in the web.config specifying the number of simultaneous outgoing web requests. As far as I can remember the default for the number was really low I believe it was 2