我有一种情况,我正在async调用返回和IDisposable实例的方法.例如:
HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.google.com"));
Run Code Online (Sandbox Code Playgroud)
现在async,在使用IDisposable实例之前,使用"response"变量的调用和代码将包含在using语句中.
我的问题是,当async混合中抛出关键字时,这仍然是正确的方法吗?即使代码编译,using语句是否仍然可以在下面的示例中按预期工作?
例1
using(HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.google.com")))
{
// Do something with the response
return true;
}
Run Code Online (Sandbox Code Playgroud)
例2
using(HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.google.com")))
{
await this.responseLogger.LogResponseAsync(response);
return true;
}
Run Code Online (Sandbox Code Playgroud)