Reputation: 1045
i am developing a wp8 app, i use a HttpClient to perform PostAsync and GetAsync operations, i am setting the timeout to 1 second :
private HttpClient client = new HttpClient();
client.Timeout = TimeSpan.FromMilliseconds(1000);
I have a try catch block on my Get and Post operation to caught the TimeOutExceptions as:
try
{
var response = await client.PostAsync(param1,param2);
}
catch (TimeoutException e)
{
//do something
}
Nevertheless my catch block is not capturing the exception, i debug my app and watch the throwen exception is a TaskCanceledException, ¿How can i caught the right exception?, ¿Why is the TimeOutException replaced?
Finally, and to avoid confusion, my real timeout will be 10 seconds, i am using 1 seconds just to test, and i need to show a message to the user if the timeout is exceeded.
Upvotes: 2
Views: 1448
Reputation: 186
On the HttpClicent PostAsync, the timeout is not sent as a TimeoutException. It is sent as a TaskCanceledException.
It is not 100% clear from the documentation, that I have seen, but the behaviour you are getting is the correct behavior. When the timeout is reached, TaskCanceledException is thrown.
This makes a little bit of sense if you look here | HttpClicent.Timeout Property
You may also set different timeouts for individual requests using a CancellationTokenSource on a task.
Upvotes: 1