123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- // /* **********************************************************************************
- // *
- // * Copyright (c) Sky Sanders. All rights reserved.
- // *
- // * This source code is subject to terms and conditions of the Microsoft Public
- // * License (Ms-PL). A copy of the license can be found in the license.htm file
- // * included in this distribution.
- // *
- // * You must not remove this notice, or any other, from this software.
- // *
- // * **********************************************************************************/
- using System;
- using System.IO;
- using System.Net;
- namespace CassiniDev
- {
- public class CassiniDevServer
- {
- private Server _server;
- public Server Server
- {
- get
- {
- return _server;
- }
- }
- public string ApplicationPath
- {
- get { return _server.PhysicalPath; }
- }
- /// <summary>
- /// The root URL of the running web application
- /// </summary>
- public string RootUrl
- {
- get
- {
- return new UriBuilder
- {
- Scheme = "http",
- Host = _server.HostName,
- Port = _server.Port,
- Path = _server.VirtualPath
- }.ToString();
- }
- }
- /// <summary>
- /// Combine the RootUrl of the running web application with the relative url
- /// specified.
- /// </summary>
- /// <param name="relativeUrl"></param>
- /// <returns></returns>
- public string NormalizeUrl(string relativeUrl)
- {
- return CassiniNetworkUtils.NormalizeUrl(RootUrl, relativeUrl);
- }
- /// <summary>
- /// Will start specified application as "localhost" on loopback and first available port in the range 8000-10000 with vpath "/"
-
- /// </summary>
- /// <param name="applicationPath">Physical path to application.</param>
- /// <param name="virtualPath">Optional. defaults to "/"</param>
- public void StartServer(string applicationPath, string virtualPath)
- {
- StartServer(applicationPath, CassiniNetworkUtils.GetAvailablePort(8000, 10000, IPAddress.Loopback, true), virtualPath, "localhost");
- }
- /// <summary>
- /// Will start specified application as "localhost" on loopback and first available port in the range 8000-10000 with vpath "/"
- /// </summary>
- /// <param name="applicationPath">Physical path to application.</param>
- public void StartServer(string applicationPath)
- {
- StartServer(applicationPath, CassiniNetworkUtils.GetAvailablePort(8000, 10000, IPAddress.Loopback, true), "/", "localhost");
- }
- /// <summary>
- /// Will start specified application on loopback
- /// </summary>
- /// <param name="applicationPath">Physical path to application.</param>
- /// <param name="port">Port to listen on.</param>
- /// <param name="virtualPath">Optional. defaults to "/"</param>
- /// <param name="hostName">Optional. Is used to construct RootUrl. Defaults to "localhost"</param>
- public void StartServer(string applicationPath, int port, string virtualPath, string hostName)
- {
- // WebHost.Server will not run on any other IP
- IPAddress ipAddress = IPAddress.Loopback;
- if (!CassiniNetworkUtils.IsPortAvailable(ipAddress, port))
- {
- throw new Exception(string.Format("Port {0} is in use.", port));
- }
- applicationPath = Path.GetFullPath(applicationPath);
- virtualPath = String.Format("/{0}/", (virtualPath ?? string.Empty).Trim('/')).Replace("//", "/");
- hostName = string.IsNullOrEmpty(hostName) ? "localhost" : hostName;
- StartServer(applicationPath, ipAddress, port, virtualPath, hostName);
- }
- /// <summary>
- /// </summary>
- /// <param name="applicationPath">Physical path to application.</param>
- /// <param name="ipAddress">IP to listen on.</param>
- /// <param name="port">Port to listen on.</param>
- /// <param name="virtualPath">Optional. default value '/'</param>
- /// <param name="hostname">Optional. Used to construct RootUrl. Defaults to 'localhost'</param>
- public void StartServer(string applicationPath, IPAddress ipAddress, int port, string virtualPath, string hostname)
- {
- if (_server != null)
- {
- throw new InvalidOperationException("Server already started");
- }
- _server = new Server(port, virtualPath, applicationPath, ipAddress, hostname, 60000);
- try
- {
- _server.Start();
- }
- catch (Exception ex)
- {
- throw new InvalidOperationException("Error starting server instance.", ex);
- }
- }
- /// <summary>
- /// <para>Stops the server.</para>
- /// </summary>
- public void StopServer()
- {
- if (_server != null)
- {
- try
- {
- _server.ShutDown();
- }
- catch
- {
- }
- }
- }
- /// <summary>
- /// </summary>
- public void Dispose()
- {
- if (_server != null)
- {
- try
- {
- StopServer();
- _server.Dispose();
- _server = null;
- }
- catch
- {
- }
- }
- }
- }
- }
|