private void EndRequest(OwinHttpListenerContext owinContext, Exception ex) { // TODO: Log the exception, if any Interlocked.Decrement(ref _currentOutstandingRequests); if (owinContext != null) { owinContext.End(ex); owinContext.Dispose(); } // Make sure we start the next request on a new thread, need to prevent stack overflows. OffloadStartNextRequest(); }
private void StartProcessingRequest(HttpListenerContext context) { Interlocked.Decrement(ref _currentOutstandingAccepts); Interlocked.Increment(ref _currentOutstandingRequests); OffloadStartNextRequest(); OwinHttpListenerContext owinContext = null; try { string pathBase, path, query; GetPathAndQuery(context.Request.RawUrl, out pathBase, out path, out query); owinContext = new OwinHttpListenerContext(context, pathBase, path, query, _disconnectHandler); PopulateServerKeys(owinContext.Environment); Contract.Assert(!owinContext.Environment.IsExtraDictionaryCreated, "All keys set by the server should have reserved slots."); _appFunc(owinContext.Environment) .Then((Func<Task>)owinContext.Response.CompleteResponseAsync, runSynchronously: true) .Then(() => { owinContext.Response.Close(); EndRequest(owinContext, null); }, runSynchronously: true) .Catch(errorInfo => { EndRequest(owinContext, errorInfo.Exception); return errorInfo.Handled(); }); } catch (Exception ex) { // TODO: Katana#5 - Don't catch everything, only catch what we think we can handle? Otherwise crash the process. EndRequest(owinContext, ex); } }