我有一个带有许多端点的api会产生我想要流式传输的惰性IEnumerable
。默认情况下,webapi将缓冲整个数据集。我已经使用PushStreamContent
编写了一些coontrollers,但感觉我做错了很多端点看起来像这样::
[HttpGet,Route("someaction/{id}")]
public async Task<HttpResponseMessage> DoSomeAction(int id,CancellationToken token)
{
var data = await GetDataIterator(id);
if(data == null) return await BadRequest(...).ExecuteAsync(token);
var response = Request.CreateResponse(HttpStatusCode.OK);
var formatter = Configuration.Formatters.First();
response.Content = new PushStreamContent(async (stream, content, transport) =>
{
await typeFormatter.WriteToStreamAsync(data.GetType(), data, stream,
content, transport, token);
stream.Close();
}, "application/json");
return response;
}
虽然我看到如果我将内容设置为Stream
它会起作用,但是如何使它成为控制器的默认行为,以便我不必继续编写相同的shell?