/// <summary> /// 启动服务器 /// </summary> /// <param name="handler"></param> /// <returns></returns> public async Task StartAsync(RequestDelegate handler) { #region 启动监听 Array.ForEach(_urls, url => _httpListener.Prefixes.Add(url)); _httpListener.Start(); Console.WriteLine("Server started and is listening on:{0}", string.Join(';', _urls)); #endregion while (true) { //获取监听的上下文 var listenerContext = await _httpListener.GetContextAsync(); var feature = new HttpListenerFeature(listenerContext); var features = new FeatureCollection() .Set <IHttpRequestFeature>(feature) .Set <IHttpResponseFeature>(feature); var httpContext = new HttpContext(features); await handler(httpContext); listenerContext.Response.Close(); } }
public async Task StartAsync(RequestDelegate handler) { Array.ForEach(_urls, url => _httpListener.Prefixes.Add(url)); _httpListener.Start(); while (true) { var listenerContext = await _httpListener.GetContextAsync(); var feature = new HttpListenerFeature(listenerContext); var features = new FeatureCollection() .Set <IHttpRequestFeature>(feature) .Set <IHttpResponseFeature>(feature); var httpContext = new HttpContext(features); await handler(httpContext); listenerContext.Response.Close(); } }