/// <summary> /// HttpListenerがリクエストを待ち受けます。 /// </summary> private async void WaitRequest() { try { while (_httpListener != null && _httpListener.IsListening) { // リクエストを受け付ける var ctx = await _httpListener.GetContextAsync(); Console.WriteLine("{0} | {1} | {2}", DateTime.Now.ToString("u"), ctx.Request.RemoteEndPoint, ctx.Request.Url); // ASP.NET のランタイムパイプラインに投げる var workerRequest = new HttpListenerWorkerRequest(ctx); HttpRuntime.ProcessRequest(workerRequest); } } catch (ObjectDisposedException) { // 待ち受けてるのがキャンセルされるとここに来る } }
/// <summary> /// HttpListenerがリクエストを受け取った時のコールバックメソッドです。 /// </summary> /// <param name="ar"></param> private void OnRequestReceived(IAsyncResult ar) { if (_httpListener == null) return; var ctx = _httpListener.EndGetContext(ar); Console.WriteLine("{0} | {1} | {2}", DateTime.Now.ToString("u"), ctx.Request.RemoteEndPoint, ctx.Request.Url); if (_httpListener.IsListening) _httpListener.BeginGetContext(OnRequestReceived, null); // ASP.NET のランタイムパイプラインに投げる var workerRequest = new HttpListenerWorkerRequest(ctx); HttpRuntime.ProcessRequest(workerRequest); }