Ejemplo n.º 1
0
        private void Stop()
        {
            try
            {
                lock (_internalLock)
                {
                    CheckDisposed();
                    if (_state == State.Stopped)
                    {
                        return;
                    }

                    LogHelper.LogTrace(Logger, "Stopping the listener.");

                    // If this instance created the queue then remove the URL prefixes before shutting down.
                    if (_requestQueue.Created)
                    {
                        Options.UrlPrefixes.UnregisterAllPrefixes();
                        _requestQueue.DetachFromUrlGroup();
                    }

                    _state = State.Stopped;
                }
            }
            catch (Exception exception)
            {
                LogHelper.LogException(Logger, "Stop", exception);
                throw;
            }
        }
Ejemplo n.º 2
0
        private void Dispose(bool disposing)
        {
            if (!disposing)
            {
                return;
            }

            lock (_internalLock)
            {
                try
                {
                    if (_state == State.Disposed)
                    {
                        return;
                    }
                    LogHelper.LogTrace(Logger, "Disposing the listener.");

                    Stop();
                    DisposeInternal();
                }
                catch (Exception exception)
                {
                    LogHelper.LogException(Logger, "Dispose", exception);
                    throw;
                }
                finally
                {
                    _state = State.Disposed;
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Start accepting incoming requests.
        /// </summary>
        public void Start()
        {
            CheckDisposed();

            LogHelper.LogTrace(Logger, "Starting the listener.");

            // Make sure there are no race conditions between Start/Stop/Abort/Close/Dispose.
            // Start needs to setup all resources. Abort/Stop must not interfere while Start is
            // allocating those resources.
            lock (_internalLock)
            {
                try
                {
                    CheckDisposed();
                    if (_state == State.Started)
                    {
                        return;
                    }

                    // If this instance created the queue then configure it.
                    if (_requestQueue.Created)
                    {
                        Options.Apply(UrlGroup, RequestQueue);

                        _requestQueue.AttachToUrlGroup();

                        // All resources are set up correctly. Now add all prefixes.
                        try
                        {
                            Options.UrlPrefixes.RegisterAllPrefixes(UrlGroup);
                        }
                        catch (HttpSysException)
                        {
                            // If an error occurred while adding prefixes, free all resources allocated by previous steps.
                            _requestQueue.DetachFromUrlGroup();
                            throw;
                        }
                    }

                    _state = State.Started;
                }
                catch (Exception exception)
                {
                    // Make sure the HttpListener instance can't be used if Start() failed.
                    _state = State.Disposed;
                    DisposeInternal();
                    LogHelper.LogException(Logger, "Start", exception);
                    throw;
                }
            }
        }