void ListenerCallback(IAsyncResult result) { HttpListenerContext context; try { if (debug) { Console.WriteLine("Processing request"); } HttpListener listener = (HttpListener)result.AsyncState; // Call EndGetContext to complete the asynchronous operation. context = listener.EndGetContext(result); try { svc.ProcessRequest(context); } catch (InvalidOperationException ex) { common.LogMessage(ex); } context.Response.Close(); listener = null; } catch (Exception ex) { common.LogMessage(ex); } finally { context = null; GC.Collect(); } }
public void Run() { try { while (_running) { var context = _lstner.GetContext(); context.Response.Headers.Add("BarHeader", "BarValue"); context.Response.Cookies.Add(new Cookie("FooCookie", "FooValue")); _svc.ProcessRequest(context); } } catch (HttpListenerException) { } }
static void ProcessRequest(IAsyncResult result) { HttpListener listener = result.AsyncState as HttpListener; // 结束异步操作 HttpListenerContext context = listener.EndGetContext(result); // 重新启动异步请求处理 listener.BeginGetContext(ProcessRequest, listener); try { // 处理请求 _svc.ProcessRequest(context); } catch (Exception) { } }