Пример #1
0
        public bool StartWebServer(string webRoot, int port)
        {
            if (_server != null) {
                throw new InvalidOperationException("The server has already been started.");
            }
            if (String.IsNullOrEmpty(webRoot)) {
                throw new ArgumentNullException("webRoot");
            }
            if (Directory.Exists(webRoot) == false) {
                throw new ArgumentException("Invalid directory specified as the web root.", "webRoot");
            }

            bool started = false;
            try {
                Server server = new Server(port, "/", webRoot);
                server.Start();

                _server = server;
                started = true;
            }
            catch (Exception e) {
                throw new InvalidOperationException("Failed to start server.", e);
            }

            if (started) {
                UriBuilder uriBuilder = new UriBuilder();
                uriBuilder.Scheme = "http";
                uriBuilder.Host = "localhost";
                uriBuilder.Port = port;

                _rootUri = uriBuilder.Uri;
            }

            return started;
        }