I write data to http response using PushStreamContent
class. I need to retrieve an appropriate HTTP status code depends on result of onStreamAvailable
delegate execution. Here is an example code:
[HttpGet]
public HttpResponseMessage Get(int id)
{
try
{
HttpResponseMessage response = this.Request.CreateResponse();
response.Content = new PushStreamContent((Stream outputStream, HttpContent content, TransportContext context) =>
{
try
{
throw new Exception("Just an exception");
response.StatusCode = HttpStatusCode.OK;
}
catch (Exception ex)
{
using (StreamWriter sw = new StreamWriter(outputStream))
{
sw.WriteLine(ex.Message);
sw.Flush();
}
response.StatusCode = HttpStatusCode.InternalServerError;
}
});
return response;
}
catch (Exception ex)
{
return this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
}
}
The example above always retrieves status code 200 (Ok). How I can fix it?