public bool StopWebServer() { bool stopped = false; if (_server != null) { try { _server.Stop(); stopped = true; } catch { } _server = null; } return(stopped); }
public bool StartWebServer(int port, params string[] webRoots) { if (_server != null) { throw new InvalidOperationException("The server has already been started."); } if (webRoots == null) { throw new ArgumentNullException("webRoots"); } foreach (string webRoot in webRoots) { if (Directory.Exists(webRoot) == false) { throw new ArgumentException("Invalid directory specified as the web root: '" + webRoot + "'"); } } bool started = false; try { WebTestHttpServer server = new WebTestHttpServer(webRoots); server.Start(port); _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); }