Andrew Kalashnikov
Andrew Kalashnikov

Reputation: 3173

.net. Max number of async HttpWebRequest

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

Upvotes: 1

Views: 1118

Answers (2)

Rotem
Rotem

Reputation: 21917

It could be an issue of the service point manager's default connection limit being too low.

(@mfeingold: is this the setting you mean? I think it defaults to 2) try setting the property:

System.Net.ServicePointManager.DefaultConnectionLimit

to something higher before creating the HttpWebRequest.

Upvotes: 3

mfeingold
mfeingold

Reputation: 7154

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

Upvotes: 2

Related Questions