ฉันมีสถานการณ์ที่ฉันasyncโทรไปยังเมธอดที่ส่งคืนและIDisposableอินสแตนซ์ ตัวอย่างเช่น:
HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.google.com"));
ก่อนหน้าasyncนี้อยู่ในที่เกิดเหตุเมื่อทำงานกับIDisposableอินสแตนซ์การโทรและรหัสที่ใช้ตัวแปร "response" นี้จะรวมอยู่ในคำสั่งใช้
คำถามของฉันคือยังคงเป็นแนวทางที่ถูกต้องหรือไม่เมื่อ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;
}