I have a controller action that returns a CSV document produced asynchronously by a data source. I subscribed to an observable data stream and I write to the http response with Response.WriteAsync(). It seems to work apart of the fact that the terminating (empty) chunk is missing at the end of the response (The missing data in hex is 30 0d 0a 0d 0a
for the terminating 0\r\n\r\n
).
I have few questions: - is it okay to use WriteAsync in this scenario - call it multiple times as the data comes in - i certainly do not want to buffer the whole dataset and return it at once as it can grow huge - whose responsibility it is to write the terminating chunk (should it write it explicitly in OnComplete handler with WriteAsync as well?)
example response from an action that returns a json object (note the terminator at the end of the response):
HTTP/1.1 200 OK
Date: Tue, 05 Feb 2019 09:25:23 GMT
Server: Kestrel
Transfer-Encoding: chunked
21
{"results":[{"statement_id":0}]}
0
Response from my action:
HTTP/1.1 200 OK
Date: Tue, 05 Feb 2019 09:27:42 GMT
Content-Type: text/csv
Server: Kestrel
Transfer-Encoding: chunked
47
ID,Timestamp,BadQualityCount,MAX_value,MEAN_value,MIN_value,TotalPoints
65
...
5d
SwitchStatus_On_a797c2c2-de78-4fe8-9e4b-5d64f51d1e00,2019-0204T11:52:17.3680000Z,0,0,0,0,1
Controller code:
using (var disposable = observableContent.Subscribe(
async current =>
await Response.WriteAsync(current),
() =>
{
//Response.WriteAsync(Environment.NewLine);
//Response.WriteAsync("0");
Response.Body.Flush();
completed = true;
}
))
SpinWait.SpinUntil(() => completed);
return Ok();