最佳答案
我遇到了这样一种情况: 我对返回的方法和 IDisposable
实例进行 async
调用。例如:
HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.google.com"));
现在,在 async
出现之前,当使用 IDisposable
实例时,这个调用和使用“ response”变量的代码将包装在 using 语句中。
我的问题是,当 async
关键字被混合在一起时,这是否仍然是正确的方法?即使代码已经编译,using 语句在下面的两个示例中是否仍然能够正常工作?
例子一
using(HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.google.com")))
{
// Do something with the response
return true;
}
例子2
using(HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.google.com")))
{
await this.responseLogger.LogResponseAsync(response);
return true;
}