Пример #1
0
        /// <summary>
        /// Start the http listener.
        /// </summary>
        public void Start()
        {
            try
            {
                // Create a new http listener
                _listener = new HttpListener();

                // Add URI prefixes to listen for.
                foreach (string uri in _uriList)
                {
                    _listener.Prefixes.Add(uri);
                }

                // Set the Authentication Schemes.
                _listener.AuthenticationSchemes = _authenticationSchemes;
                if (_authenticationSchemeSelectorDelegate != null)
                {
                    _listener.AuthenticationSchemeSelectorDelegate = _authenticationSchemeSelectorDelegate;
                }

                // Get the mime types
                _contextMimeType = ReaderHttp.GetMimeType();

                // Load all the composition assemblies.
                _composition = new Composition();
                _composition.Compose();

                // Start the listener
                _listener.Start();

                // Keep the service in the running start
                // listen for in-comming requests.
                while (_running)
                {
                    // Start a new listening thread for the
                    // current connection request.
                    IAsyncResult result = _listener.BeginGetContext(new AsyncCallback(AsynchronousListenerCallback), _listener);

                    // Wait until the current context is made and processed before continuing.
                    result.AsyncWaitHandle.WaitOne();
                }

                // If the runServer flag gets set to false,
                // stop the server and close the listener.
                _listener.Close();
            }
            catch (Exception ex)
            {
                // Log the error.
                LogHandler.WriteTypeMessage(
                    ex.Message,
                    MethodInfo.GetCurrentMethod(),
                    Nequeo.Net.Common.Helper.EventApplicationName);
            }
            finally
            {
                if (_listener != null)
                {
                    _listener.Close();
                }

                try
                {
                    _composition.Compose(false);
                    _composition = null;
                }
                catch (Exception ex)
                {
                    // Log the error.
                    LogHandler.WriteTypeMessage(
                        ex.Message,
                        MethodInfo.GetCurrentMethod(),
                        Nequeo.Net.Common.Helper.EventApplicationName);
                }
            }
        }