อะไรคือลายเซ็นที่ถูกต้องสำหรับแอ็คชั่นคอนโทรลเลอร์ที่ส่งกลับค่าIAsyncEnumerable<T>a และ a NotFoundResultแต่ยังคงถูกประมวลผลในรูปแบบ async?
ฉันใช้ลายเซ็นนี้และไม่ได้รวบรวมเพราะIAsyncEnumerable<T>ไม่สามารถรอได้:
[HttpGet]
public async Task<IActionResult> GetAll(Guid id)
{
try
{
return Ok(await repository.GetAll(id)); // GetAll() returns an IAsyncEnumerable
}
catch (NotFoundException e)
{
return NotFound(e.Message);
}
}
อันนี้คอมไพล์ได้ดี แต่ลายเซ็นมันไม่ได้เป็นแบบซิงค์ ดังนั้นฉันกังวลว่าจะบล็อกเธรดพูลเธรดหรือไม่:
[HttpGet]
public IActionResult GetAll(Guid id)
{
try
{
return Ok(repository.GetAll(id)); // GetAll() returns an IAsyncEnumerable
}
catch (NotFoundException e)
{
return NotFound(e.Message);
}
}
ฉันพยายามใช้await foreachวนรอบเช่นนี้ แต่ที่เห็นได้ชัดว่าจะไม่รวบรวม:
[HttpGet]
public async IAsyncEnumerable<MyObject> GetAll(Guid id)
{
IAsyncEnumerable<MyObject> objects;
try
{
objects = contentDeliveryManagementService.GetAll(id); // GetAll() returns an IAsyncEnumerable
}
catch (DeviceNotFoundException e)
{
return NotFound(e.Message);
}
await foreach (var obj in objects)
{
yield return obj;
}
}
IAsyncEnumerableกำลังรอคอย await foreach(var item from ThatMethodAsync()){...}ใช้
IAsyncEnumerable<MyObject> return objectsแต่นั่นจะไม่แปลงการกระทำ HTTP เป็นวิธีสตรีมมิ่ง gRPC หรือ SignalR มิดเดิลแวร์จะยังคงใช้ข้อมูลและส่งการตอบกลับ HTTP เดียวไปยังไคลเอนต์
IAsyncEnumerableรู้ถึง 3.0
MyObjectสินค้าหลายรายการด้วยidหรือไม่ โดยปกติแล้วคุณจะไม่ส่งNotFoundบางอย่างที่ส่งกลับIEnumerable- มันก็จะว่างเปล่า - หรือคุณจะกลับรายการเดียวกับที่มีการร้องขอ/idNotFound