76
我有一種情況,我正在調用一個返回的方法和IDisposable
實例。例如:使用語句和等待關鍵字在c#中很好地播放使用#
HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.google.com"));
現在擺在async
是在現場,與IDisposable
情況下工作時,這種通話和代碼所用的「反應」變量將被包裹在一個using語句。
我的問題是,當關鍵字async
被混入時,這是否仍然是正確的方法?即使代碼編譯完成,使用語句在下面的兩個例子中是否仍能按預期工作?
實施例1對
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;
}
感謝Jon。雖然大多數異步內容對我來說仍然是伏都教,但看到它與其他.net功能集成的頻率並且只是起作用 – swingdoctor