I have a situation where I am making an async
call to a method that returns and IDisposable
instance. For example:
HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.google.com"));
Now before async
was on the scene, when working with an IDisposable
instance, this call and code that used the "response" variable would be wrapped in a using statement.
My question is whether that is still the correct approach when the async
keyword is thrown in the mix? Even though the code compiles, will the using statement still work as expected in both the examples below?
Example 1
using(HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.google.com")))
{
// Do something with the response
return true;
}
Example 2
using(HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.google.com")))
{
await this.responseLogger.LogResponseAsync(response);
return true;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…