Hi,
I'm using PushStreamContent in asp.net web api to stream data to the client, I have derived from ExceptionFilterAttribute to capture all the exceptions and return standard HttpError to client. However,
I found that exception thrown inside PushStreamContent won't be caught by ExceptionFilterAttribute, it will return a "text/html" error message to client.
Below is the sample code:
response.Content = new PushStreamContent(
async (outputStream, httpContent, transportContext) =>
{
try
{
// do something and throw exception here
}
catch (Exception ex)
{
throw;
}
finally
{
outputStream.Close();
}
});
Comments: Then is there any best practice to handle exceptions from PushStreamContent? For example, client use code below to read the stream using (Stream stream = response.GetResponseStream()) { int count; while ((count = stream.Read(data, totalCount, data.Length - totalCount)) > 0) { totalCount += count; } } However, client will read the exception content as normal stream which is not expected.
I'm using PushStreamContent in asp.net web api to stream data to the client, I have derived from ExceptionFilterAttribute to capture all the exceptions and return standard HttpError to client. However,
I found that exception thrown inside PushStreamContent won't be caught by ExceptionFilterAttribute, it will return a "text/html" error message to client.
Below is the sample code:
response.Content = new PushStreamContent(
async (outputStream, httpContent, transportContext) =>
{
try
{
// do something and throw exception here
}
catch (Exception ex)
{
throw;
}
finally
{
outputStream.Close();
}
});
Comments: Then is there any best practice to handle exceptions from PushStreamContent? For example, client use code below to read the stream using (Stream stream = response.GetResponseStream()) { int count; while ((count = stream.Read(data, totalCount, data.Length - totalCount)) > 0) { totalCount += count; } } However, client will read the exception content as normal stream which is not expected.