public static void ListenerCallback(IAsyncResult result) { HttpListener listener = (HttpListener)result.AsyncState; // Use EndGetContext to complete the asynchronous operation. HttpListenerContext ctx; try { ctx = listener.EndGetContext(result); } catch (ObjectDisposedException ex) { return; } object func = null; try { string rawPath = ctx.Request.RawUrl; if (rawPath.Contains("?")) { rawPath = rawPath.Substring(0, rawPath.IndexOf("?")); } var pathInfo = PathTree.getPath(rawPath); var path = pathInfo.VariablePath; string body = null; if ("POST".Equals(ctx.Request.HttpMethod) && postMappings.ContainsKey(path)) { func = postMappings[path]; } else if ("GET".Equals(ctx.Request.HttpMethod) && getMappings.ContainsKey(path)) { func = getMappings[path]; } else if ("DELETE".Equals(ctx.Request.HttpMethod) && deleteMappings.ContainsKey(path)) { func = deleteMappings[path]; } else if ("PUT".Equals(ctx.Request.HttpMethod) && putMappings.ContainsKey(path)) { func = putMappings[path]; } if (func == null) { throw new HttpStatusAwareException(404, "Not Found"); } if (func is Func <EagleRequest, HttpListenerResponse, string> ) { var request = new EagleRequest { Body = getBodyDynamic(ctx.Request), PathInfo = pathInfo, RawRequest = ctx.Request, QueryParams = ctx.Request.QueryString }; body = ((Func <EagleRequest, HttpListenerResponse, string>)func)(request, ctx.Response); } else if (func is Func <EagleRequest, HttpListenerResponse, object> ) { var request = new EagleRequest { Body = getBodyDynamic(ctx.Request), PathInfo = pathInfo, RawRequest = ctx.Request, QueryParams = ctx.Request.QueryString }; var obj = ((Func <EagleRequest, HttpListenerResponse, object>)func)(request, ctx.Response); ctx.Response.ContentType = "application/json"; body = Newtonsoft.Json.JsonConvert.SerializeObject(obj); } else if (func is Action <EagleRequest, HttpListenerResponse> ) { var request = new EagleRequest { Body = getBodyDynamic(ctx.Request), PathInfo = pathInfo, RawRequest = ctx.Request, QueryParams = ctx.Request.QueryString }; ((Action <EagleRequest, HttpListenerResponse>)func)(request, ctx.Response); body = ""; } reply(ctx.Response, body); } catch (HttpStatusAwareException ex) { reply(ctx.Response, ex); } catch (Exception ex) { reply(ctx.Response, new HttpStatusAwareException(500, "internal server error")); } finally { } }