I'am trying to call GetAsync multiple times in a nonblocking async function. To my surprise only the fist call gets executed. All requests are to the same domain so I figured it makes sense to reuse HttpClient
. Why is only the first request executed and how should I rewrite the code?
private static HttpClient client = new HttpClient(new HttpClientHandler(){UseProxy = false});
private static async Task NonblockingGet(string destination){
client.Timeout = TimeSpan.FromSeconds(10);
var result = await client.GetAsync(destination);
// handle result
}
private static void CallMultipleTimes(){
NonblockingGet("domain1/url1"); // only this one is executed
NonblockingGet("domain1/url2");
NonblockingGet("domain1/url3");
}
//main
ManualResetEvent mre = new ManualResetEvent(false);
CallMultipleTimes();
mre.WaitOne();
No, all three calls to NonblockingGet
are executed. But on the second call, you are trying to modify the client
(ie set the Timeout) after there was already a request started. That's not allowed and it throws a System.InvalidOperationException
exception (which is silently ignored)
This instance has already started one or more requests. Properties can only be modified before sending the first request.
Thus, of course, the second and the third client.GetAsync()
are not executed.
Move the client.Timeout = TimeSpan.FromSeconds(10);
to be the first statement in CallMultipleTimes()
(or somewhere else where it's only executed once before the very first request) and everything will work as expected (at least for this usecase).
private static async Task NonblockingGet(string destination){
// client.Timeout = TimeSpan.FromSeconds(10); // <--- remove it here
var result = await client.GetAsync(destination);
// handle result
}
private static void CallMultipleTimes(){
client.Timeout = TimeSpan.FromSeconds(10); // <--- add it here
NonblockingGet("domain1/url1"); // only this one is executed
NonblockingGet("domain1/url2");
NonblockingGet("domain1/url3");
}