private void ProcessRequest(IAsyncResult ar) { HttpListenerContext listenerContext; try { logger.Debug("Retrieving request context"); listenerContext = listener.EndGetContext(ar); } catch (Exception ex) { logger.Error("Bad request, could not retrieve context.", ex); logger.Debug("Waiting for next request"); listener.BeginGetContext(ProcessRequest, null); return; } var httpContext = new WrappedHttpListenerContext(this, listenerContext); try { logger.Info("Incoming request: {0} - {1}", httpContext.Request.HttpMethod, httpContext.Request.Url); if (httpContext.Request.Url.AbsolutePath.TrimStart('/') == "") { logger.Info("Replacing url with default path: {0}", httpContext.ServerInfo.DefaultPath); httpContext.Request.ReplaceUrl(httpContext.ServerInfo.DefaultPath); } var handler = handlers.FirstOrDefault(x => x.HandleRequest(httpContext)); if (handler == null) { // TODO } else { logger.Debug("Request handled by {0}", handler.ToString()); } } catch (Exception ex) { HandleException(ex, httpContext); } finally { httpContext.Response.Dispose(); logger.Debug("Waiting for next request"); listener.BeginGetContext(ProcessRequest, null); } }
private void ProcessRequest(IAsyncResult ar) { HttpListenerContext listenerContext; try { logger.Debug("Retrieving request context"); listenerContext = listener.EndGetContext(ar); } catch (Exception ex) { logger.Error("Bad request, could not retrieve context.", ex); logger.Debug("Waiting for next request"); listener.BeginGetContext(ProcessRequest, null); return; } var httpContext = new WrappedHttpListenerContext(this, listenerContext); try { logger.Info("Incoming request: {0} - {1}", httpContext.Request.HttpMethod, httpContext.Request.Url); if (httpContext.Request.Url.AbsolutePath.TrimStart('/') == "") { logger.Info("Replacing url with default path: {0}", httpContext.ServerInfo.DefaultPath); httpContext.Request.ReplaceUrl(httpContext.ServerInfo.DefaultPath); } var handler = handlers.FirstOrDefault(x => x.HandleRequest(httpContext)); if (handler == null) { // TODO } else { logger.Debug("Request handled by {0}", handler.ToString()); } } catch(Exception ex) { HandleException(ex, httpContext); } finally { httpContext.Response.Dispose(); logger.Debug("Waiting for next request"); listener.BeginGetContext(ProcessRequest, null); } }