我試圖遷移一個web api項目(經典web.config項目)那裏使用PushStreamContent到最新的asp.net 5 web應用程序(project.json)。在asp.net 5/mvc 6 PushStreamContent不工作
我的問題是,我不能讓PushStreamContent工作。
當我使用這個API控制器 - 結果將在一個JSON格式的結束,而不是作爲一個流:
[Route("api/[controller]")]
public class EventsController : Controller
{
private static readonly ConcurrentQueue<StreamWriter> s_streamWriter = new ConcurrentQueue<StreamWriter>();
[HttpGet]
public HttpResponseMessage Get(HttpRequestMessage request)
{
HttpResponseMessage response = request.CreateResponse();
response.Content = new PushStreamContent(new Action<Stream, HttpContent, TransportContext>(WriteToStream), "text/event-stream");
return response;
}
private void WriteToStream(Stream outputStream, HttpContent headers, TransportContext context)
{
var streamWriter = new StreamWriter(outputStream) {AutoFlush = true};
s_streamWriter.Enqueue(streamWriter);
}
}
如果我改變控制器動作返回一個任務,並在一類MyPushStreamResult包裝PushStreamContent - 像這樣:
[HttpGet]
public async Task<IActionResult> Get(HttpRequestMessage request)
{
var stream = new PushStreamContent(new Action<Stream, HttpContent, TransportContext>(WriteToStream), "text/event-stream");
return new MyPushStreamResult(stream, "text/event-stream");
}
public class MyPushStreamResult : ActionResult
{
public string ContentType { get; private set; }
public PushStreamContent Stream { get; private set; }
public MyPushStreamResult(PushStreamContent stream, string contentType)
{
Stream = stream;
ContentType = contentType;
}
public override async Task ExecuteResultAsync(ActionContext context)
{
var response = context.HttpContext.Response;
response.ContentType = ContentType;
await Stream.CopyToAsync(response.Body);
}
}
到我的控制器操作的請求,現在返回一個流,但該流不刷新它接近的服務器端或者包含大量數據的之前。當我將數據推送到PushStreamContent輸出流時,我在每次寫入文本後進行刷新,但我猜刷新不在response.Body流中。
我錯過了什麼?找不到任何具有asp.net 5結構的示例。
'IHttpBufferingFeature'功能可以幫助我想,你是否嘗試禁用緩衝? –
它看起來像'IHttpBufferingFeature'不支持Kestrel [文檔](http://docs.asp.net/en/latest/fundamentals/servers.html#supported-features-by-server) –
在第一個例子中你從不寫信給流。 – usr