private async Task ProcessRequest(HttpListenerContext ctx) { var response = new MockHttpResponse(ctx.Response); try { var content = await ReadContentAsync(ctx); var request = new MockHttpRequest(ctx.Request, content, BaseAddress); var processor = _processors.FirstOrDefault(p => p.Match(request)) ?? NotImplementedProcessor; await processor.ProcessRequestAsync(request, response); await response.SendResponseAsync(); } catch (Exception e) { ctx.Response.StatusCode = (int)HttpStatusCode.InternalServerError; ctx.Response.ContentType = "text"; var buffer = Encoding.UTF8.GetBytes(e.ToString()); ctx.Response.ContentEncoding = Encoding.UTF8; ctx.Response.ContentLength64 = buffer.Length; ctx.Response.OutputStream.Write(buffer, 0, buffer.Length); } finally { response.Close(); } }