static void Main(string[] args) { // Parse the commandline arguments: var configValues = ConfigurationDictionary.Parse(args); // Require at least one "bind" value: List<string> bindUriPrefixes; if (!configValues.TryGetValue("bind", out bindUriPrefixes) || bindUriPrefixes.Count == 0) { Console.Error.WriteLine("Require at least one bind=http://ip:port/ argument."); return; } // Configurable number of accept requests active at one time per CPU core: int accepts = 4; string acceptsString; if (configValues.TryGetSingleValue("accepts", out acceptsString)) { if (!Int32.TryParse(acceptsString, out accepts)) accepts = 4; } else { accepts = 4; } // Create an HTTP host and start it: var handler = new APIHttpAsyncHandler(); var host = new HttpAsyncHost(handler, accepts); host.SetConfiguration(configValues); host.Run(bindUriPrefixes.ToArray()); }
static async Task ProcessListenerContext(HttpListenerContext listenerContext, HttpAsyncHost host) { Debug.Assert(listenerContext != null); try { // Get the response action to take: var requestContext = new HttpRequestContext(host._hostContext, listenerContext.Request, listenerContext.User); var action = await host._handler.Execute(requestContext); if (action != null) { // Take the action and await its completion: var responseContext = new HttpRequestResponseContext(requestContext, listenerContext.Response); var task = action.Execute(responseContext); if (task != null) await task; } // Close the response and send it to the client: listenerContext.Response.Close(); } catch (HttpListenerException) { // Ignored. } catch (Exception ex) { // TODO: better exception handling Trace.WriteLine(ex.ToString()); } }
static async Task ProcessListenerContext(HttpListenerContext listenerContext, HttpAsyncHost host) { Debug.Assert(listenerContext != null); try { // Get the response action to take: var requestContext = new HttpRequestContext(host._hostContext, listenerContext.Request, listenerContext.User); var action = await host._handler.Execute(requestContext); if (action != null) { // Take the action and await its completion: var responseContext = new HttpRequestResponseContext(requestContext, listenerContext.Response); var task = action.Execute(responseContext); if (task != null) { await task; } } // Close the response and send it to the client: listenerContext.Response.Close(); } catch (HttpListenerException) { // Ignored. } catch (Exception ex) { // TODO: better exception handling Trace.WriteLine(ex.ToString()); } }