private async void ProcessRequest(HttpListenerContext context, AppFunc app, OwinLogger logger) { OwinHttpListenerContext owinContext = null; try { GetPathAndQuery(context.Request, out var pathBase, out var path, out var query); owinContext = new OwinHttpListenerContext(context, pathBase, path, query); await app.Invoke(owinContext.Environment); if (!context.Connection.IsClosed) { owinContext.Response.CompleteResponse(); } owinContext.Response.Close(); owinContext.End(); owinContext.Dispose(); } catch (Exception ex) { if (owinContext != null) { owinContext.End(ex); owinContext.Dispose(); } } }
/// <summary> /// Called by <see cref="Microsoft.Owin.Hosting.ServerFactory.ServerFactoryAdapter"/> /// which starts the server /// </summary> /// <param name="app"></param> /// <param name="properties"></param> /// <returns></returns> public IDisposable Create(AppFunc app, IDictionary <string, object> properties) { var loggerFactory = properties.Get <LoggerFactoryFunc>(Constants.ServerLoggerFactoryKey); var logger = new OwinLogger(loggerFactory, GetType()); var listener = new HttpListener(logger); listener.OnContext = ctx => ProcessRequest(ctx, app, logger); listener.Prefixes.Add(_url); listener.Start(); return(new Disposable(listener)); }