c#


HttpResponse - how to set status code depends on PushStreamContent execution result


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?

Related Links

c# wpf getting input from textbox in dynamically generated DataTemplate objects
Loading spreadsheet template file doesn't retain form button
Openssl command with certificate in different path from OpenSSL directory
Windows Service with Autofac Holds onto References
LINQ expression with multiple methods
True being returned as false
Custom Console.WriteLine w/ Stream buffer
Any faster way to set console colors?
Custom Conversion throws exception Automapper 5.2.0
htmlagilitypack getting meta tag with content attributes
Quickest way to detect missing values in json or list of objects
Why is this parameter empty?
Retrieving Dropdown List Value in MVC [duplicate]
Placing items in List on specific spots [duplicate]
Correct item selected upon user pressing enter for a combobox
Uwp binding combobox selected value to Dependency Property

Categories

HOME
sql-server-2014
gsm
packet
popover
highmaps
symfony-sonata
browser-cache
multipartform-data
gnuradio
jquery-select2
clip-path
segmentation-fault
wkhtmltopdf
soap-client
velocity
urllib2
aspectj
watson-dialog
camunda
android-notifications
nsoperation
vivado-hls
bootstrap-datetimepicker
android-wifi
ecmascript-2017
bootstrap-accordion
core-animation
knapsack-problem
ballerinalang
paper-trail-gem
keil
rtos
intersection
anypoint-studio
form-fields
framebuffer
motion
icu
amazon-rds-aurora
azure-data-catalog
evaluation
jcs
enet
multi-targeting
try-catch-finally
trusted-computing
turbolinks
normalisation
window.location
paypal-subscriptions
stdlist
wizard
android-filterable
tooltwist
borland-c++
pdcurses
genome
genero
xmla
mind-manager
qf-test
agent-based-modeling
disabled-control
amzi-prolog
cs193p
pidgin
git-subtree
dylan
grape-api
ironworker
extensibility
cocotron
phone-state-listener
boost.build
volatility
big-endian
doxia
sup
pdf-reader
eeprom
utm
rjs
recess
cpack
html5-animation
onunload
fxcopcmd
azure-appfabric
atmega16
djangoappengine
scalable

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App