Exemplo n.º 1
0
        /// <summary>
        /// Processes the requests.
        /// </summary>
        /// <returns></returns>
        private async Task ProcessRequestAsync()
        {
            while (!_cancelTokenSource.IsCancellationRequested)
            {
                try
                {
                    var context = await _httpListener.GetContextAsync().ConfigureAwait(false);

                    try
                    {
                        await ProcessRequestAsync(context).ConfigureAwait(false);

                        context.Response.Close();
                    }
                    catch (Exception ex)
                    {
                        context.Response.StatusCode        = 500;
                        context.Response.StatusDescription = "Internal Server Error";
                        context.Response.Close();
                        _log.ErrorException("Error processing HTTP request", ex);
                    }
                }
                // ReSharper disable once UncatchableException
                catch (ObjectDisposedException ex)
                {
                    if (ex.ObjectName == _httpListener.GetType().FullName&& _httpListener.IsListening == false)
                    {
                        return; // listener is closed/disposed
                    }
                    _log.ErrorException("Error processing HTTP request", ex);
                }
                catch (Exception ex)
                {
                    if (!(ex is HttpListenerException listenerException) || listenerException.ErrorCode != 995)// IO operation aborted
                    {
                        _log.ErrorException("Error processing HTTP request", ex);
                    }
                }
            }
        }
Exemplo n.º 2
0
 private void StopListening()
 {
     _httpListener.GetType().InvokeMember("RemoveAll", BindingFlags.Instance, null, _httpListener, new object[] { false });
     _httpListener.Close();
     // pendingRequestQueue.Clear(); //this is something we use but just in case you have some requests clear them
 }