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

                Log.ListenerStopping(Logger);

                // If this instance registered URL prefixes then remove them before shutting down.
                if (Options.RequestQueueMode == RequestQueueMode.Create || Options.RequestQueueMode == RequestQueueMode.CreateOrAttach)
                {
                    Options.UrlPrefixes.UnregisterAllPrefixes();
                    UrlGroup.DetachFromQueue();
                }

                _state = State.Stopped;
            }
        }
        catch (Exception exception)
        {
            Log.ListenerStopError(Logger, exception);
            throw;
        }
    }
Example #2
0
    /// <summary>
    /// Start accepting incoming requests.
    /// </summary>
    public void Start()
    {
        CheckDisposed();

        Log.ListenerStarting(Logger);

        // 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;
                }

                // Always configure the UrlGroup if the intent was to create, only configure the queue if we actually created it
                if (Options.RequestQueueMode == RequestQueueMode.Create || Options.RequestQueueMode == RequestQueueMode.CreateOrAttach)
                {
                    Options.Apply(UrlGroup, _requestQueue.Created ? RequestQueue : null);

                    UrlGroup.AttachToQueue();

                    // 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.
                        UrlGroup.DetachFromQueue();
                        throw;
                    }
                }

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