From 54c10ad5cb88da4d68c00381631a22e5b345df44 Mon Sep 17 00:00:00 2001 From: James O'Cull Date: Fri, 5 Jan 2018 18:06:04 -0500 Subject: [PATCH] Change to callback based responses for better performance --- src/Nancy.Hosting.Self/NancyHost.cs | 31 ++++++++++++++++++----------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/Nancy.Hosting.Self/NancyHost.cs b/src/Nancy.Hosting.Self/NancyHost.cs index 16414560e5..32fe3625ab 100644 --- a/src/Nancy.Hosting.Self/NancyHost.cs +++ b/src/Nancy.Hosting.Self/NancyHost.cs @@ -426,24 +426,31 @@ private void GotCallback(IAsyncResult ar) private void Process(HttpListenerContext ctx) { - try + Action onError = (e) => { - var nancyRequest = this.ConvertRequestToNancyRequest(ctx.Request); - using (var nancyContext = this.engine.HandleRequest(nancyRequest)) + this.configuration.UnhandledExceptionCallback.Invoke(e); + }; + + Action onComplete = (nancyContext) => + { + try { - try - { - ConvertNancyResponseToResponse(nancyContext.Response, ctx.Response); - } - catch (Exception e) - { - this.configuration.UnhandledExceptionCallback.Invoke(e); - } + ConvertNancyResponseToResponse(nancyContext.Response, ctx.Response); } + catch (Exception e) + { + onError(e); + } + }; + + try + { + var nancyRequest = this.ConvertRequestToNancyRequest(ctx.Request); + this.engine.HandleRequest(nancyRequest, onComplete, onError); } catch (Exception e) { - this.configuration.UnhandledExceptionCallback.Invoke(e); + onError(e); } } }