Qual è la firma giusta per un'azione del controller che restituisce un IAsyncEnumerable<T>e un NotFoundResultma viene comunque elaborata in modo asincrono?
Ho usato questa firma e non viene compilata perché IAsyncEnumerable<T>non è prevedibile:
[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);
}
}
Questo compila bene ma la sua firma non è asincrona. Quindi sono preoccupato se bloccherà o meno i thread del pool di thread:
[HttpGet]
public IActionResult GetAll(Guid id)
{
try
{
return Ok(repository.GetAll(id)); // GetAll() returns an IAsyncEnumerable
}
catch (NotFoundException e)
{
return NotFound(e.Message);
}
}
Ho provato a usare un await foreachloop in questo modo ma ovviamente non si sarebbe nemmeno compilato:
[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è attendibile. Usa await foreach(var item from ThatMethodAsync()){...}.
IAsyncEnumerable<MyObject>, è sufficiente restituire il risultato, ad es return objects. Tuttavia, ciò non converte un'azione HTTP in un metodo gRPC o SignalR in streaming. Il middleware consumerà comunque i dati e invierà una singola risposta HTTP al client
IAsyncEnumerableaggiornato alla versione 3.0.
MyObjectarticoli con lo stessoid? Normalmente non invierestiNotFounda qualcosa che restituisce unIEnumerable- sarebbe solo vuoto - o restituiresti il singolo articolo con ilid/ richiestoNotFound.