I'm building an HTTP proxy using Web API with self-hosting:
* the self-host server is configured with a delegating handler that forwards the requests to the origin-server using HttpClient, after some minor transformations.
* when the response is received from the HttpClient, the delegating handlers returns it to the self-host server.
Unfortunately, I'm having uncaught-exceptions on a thread with only non-user code. Therefore, I'm unable to catch it and process it appropriately
```
Unhandled Exception: System.Runtime.CallbackException: Async Callback threw an exception. ---> System.ServiceModel.Comm
nicationException: An operation was attempted on a nonexistent network connection ---> System.Net.HttpListenerException
An operation was attempted on a nonexistent network connection
at System.Net.HttpResponseStream.BeginWrite(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object
tate)
at System.ServiceModel.Channels.HttpOutput.ListenerResponseHttpOutput.ListenerResponseOutputStream.BeginWrite(Byte[]
buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state)
--- End of inner exception stack trace ---
at System.ServiceModel.Channels.HttpOutput.ListenerResponseHttpOutput.ListenerResponseOutputStream.BeginWrite(Byte[]
buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state)
at System.ServiceModel.Channels.BufferedOutputAsyncStream.ByteBuffer.FlushAsync()
at System.ServiceModel.Channels.BufferedOutputAsyncStream.DequeueAndFlush(ByteBuffer currentBuffer, AsyncEventArgsCa
lback callback)
at System.ServiceModel.Channels.BufferedOutputAsyncStream.FlushPendingBuffer()
at System.ServiceModel.Channels.BufferedOutputAsyncStream.Close()
at System.ServiceModel.Channels.HttpOutput.Close()
at System.ServiceModel.Channels.HttpRequestContext.OnClose(TimeSpan timeout)
at System.ServiceModel.Channels.HttpRequestContext.ListenerHttpContext.OnClose(TimeSpan timeout)
at System.ServiceModel.Channels.RequestContextBase.Close(TimeSpan timeout)
at System.ServiceModel.Channels.RequestContextBase.Close()
at System.Web.Http.SelfHost.Channels.HttpMessageEncodingRequestContext.Close()
at System.Web.Http.SelfHost.HttpSelfHostServer.ReplyContext.Dispose(Boolean disposing)
at System.Web.Http.SelfHost.HttpSelfHostServer.ReplyComplete(IAsyncResult result)
at System.Web.Http.SelfHost.HttpSelfHostServer.OnReplyComplete(IAsyncResult result)
at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously)
--- End of inner exception stack trace ---
at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously)
at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously, Exception exception)
at System.ServiceModel.Channels.HttpRequestContext.ReplyAsyncResult.OnSendResponseCompletedCallback(IAsyncResult res
lt)
at System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)
at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously)
at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously, Exception exception)
at System.ServiceModel.Channels.HttpOutput.SendAsyncResult.OnWriteStreamedMessage(IAsyncResult result)
at System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)
at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously)
at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously, Exception exception)
at System.ServiceModel.Channels.HttpOutput.WriteStreamedMessageAsyncResult.OnWriteStreamedMessage(IAsyncResult resul
)
at System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)
at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously)
at System.Runtime.ScheduleActionItemAsyncResult.DoWork(Object state)
at System.Runtime.ActionItem.DefaultActionItem.TraceAndInvoke()
at System.Runtime.ActionItem.DefaultActionItem.Invoke()
at System.Runtime.ActionItem.CallbackHelper.InvokeWithoutContext(Object state)
at System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapp
d* nativeOverlapped)
at System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nat
veOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverl
pped* pOVERLAP)
```
I've tried returning a StreamContent and a PushStreamContent and the behavior is similar.
I believe that this is related to [issue 284](http://aspnetwebstack.codeplex.com/workitem/284), however it is still occurring for me.
This is a __blocking issue__ for me because the exception ends the server process and there doesn't seem to be a way for me to catch it.
I'm running on .NET 4.5.
Comments: Hi, We fixed this issue and pushed a new selfhost NuGet package several months ago. Can you try updating your NuGet package for selfhost and see if that fixes the issue for you? Youssef
* the self-host server is configured with a delegating handler that forwards the requests to the origin-server using HttpClient, after some minor transformations.
* when the response is received from the HttpClient, the delegating handlers returns it to the self-host server.
Unfortunately, I'm having uncaught-exceptions on a thread with only non-user code. Therefore, I'm unable to catch it and process it appropriately
```
Unhandled Exception: System.Runtime.CallbackException: Async Callback threw an exception. ---> System.ServiceModel.Comm
nicationException: An operation was attempted on a nonexistent network connection ---> System.Net.HttpListenerException
An operation was attempted on a nonexistent network connection
at System.Net.HttpResponseStream.BeginWrite(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object
tate)
at System.ServiceModel.Channels.HttpOutput.ListenerResponseHttpOutput.ListenerResponseOutputStream.BeginWrite(Byte[]
buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state)
--- End of inner exception stack trace ---
at System.ServiceModel.Channels.HttpOutput.ListenerResponseHttpOutput.ListenerResponseOutputStream.BeginWrite(Byte[]
buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state)
at System.ServiceModel.Channels.BufferedOutputAsyncStream.ByteBuffer.FlushAsync()
at System.ServiceModel.Channels.BufferedOutputAsyncStream.DequeueAndFlush(ByteBuffer currentBuffer, AsyncEventArgsCa
lback callback)
at System.ServiceModel.Channels.BufferedOutputAsyncStream.FlushPendingBuffer()
at System.ServiceModel.Channels.BufferedOutputAsyncStream.Close()
at System.ServiceModel.Channels.HttpOutput.Close()
at System.ServiceModel.Channels.HttpRequestContext.OnClose(TimeSpan timeout)
at System.ServiceModel.Channels.HttpRequestContext.ListenerHttpContext.OnClose(TimeSpan timeout)
at System.ServiceModel.Channels.RequestContextBase.Close(TimeSpan timeout)
at System.ServiceModel.Channels.RequestContextBase.Close()
at System.Web.Http.SelfHost.Channels.HttpMessageEncodingRequestContext.Close()
at System.Web.Http.SelfHost.HttpSelfHostServer.ReplyContext.Dispose(Boolean disposing)
at System.Web.Http.SelfHost.HttpSelfHostServer.ReplyComplete(IAsyncResult result)
at System.Web.Http.SelfHost.HttpSelfHostServer.OnReplyComplete(IAsyncResult result)
at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously)
--- End of inner exception stack trace ---
at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously)
at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously, Exception exception)
at System.ServiceModel.Channels.HttpRequestContext.ReplyAsyncResult.OnSendResponseCompletedCallback(IAsyncResult res
lt)
at System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)
at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously)
at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously, Exception exception)
at System.ServiceModel.Channels.HttpOutput.SendAsyncResult.OnWriteStreamedMessage(IAsyncResult result)
at System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)
at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously)
at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously, Exception exception)
at System.ServiceModel.Channels.HttpOutput.WriteStreamedMessageAsyncResult.OnWriteStreamedMessage(IAsyncResult resul
)
at System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)
at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously)
at System.Runtime.ScheduleActionItemAsyncResult.DoWork(Object state)
at System.Runtime.ActionItem.DefaultActionItem.TraceAndInvoke()
at System.Runtime.ActionItem.DefaultActionItem.Invoke()
at System.Runtime.ActionItem.CallbackHelper.InvokeWithoutContext(Object state)
at System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapp
d* nativeOverlapped)
at System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nat
veOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverl
pped* pOVERLAP)
```
I've tried returning a StreamContent and a PushStreamContent and the behavior is similar.
I believe that this is related to [issue 284](http://aspnetwebstack.codeplex.com/workitem/284), however it is still occurring for me.
This is a __blocking issue__ for me because the exception ends the server process and there doesn't seem to be a way for me to catch it.
I'm running on .NET 4.5.
Comments: Hi, We fixed this issue and pushed a new selfhost NuGet package several months ago. Can you try updating your NuGet package for selfhost and see if that fixes the issue for you? Youssef