CassiniServer.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // /* **********************************************************************************
  2. // *
  3. // * Copyright (c) Sky Sanders. All rights reserved.
  4. // *
  5. // * This source code is subject to terms and conditions of the Microsoft Public
  6. // * License (Ms-PL). A copy of the license can be found in the license.htm file
  7. // * included in this distribution.
  8. // *
  9. // * You must not remove this notice, or any other, from this software.
  10. // *
  11. // * **********************************************************************************/
  12. using System;
  13. using System.IO;
  14. using System.Net;
  15. namespace CassiniDev
  16. {
  17. public class CassiniDevServer
  18. {
  19. private Server _server;
  20. public Server Server
  21. {
  22. get
  23. {
  24. return _server;
  25. }
  26. }
  27. public string ApplicationPath
  28. {
  29. get { return _server.PhysicalPath; }
  30. }
  31. /// <summary>
  32. /// The root URL of the running web application
  33. /// </summary>
  34. public string RootUrl
  35. {
  36. get
  37. {
  38. return new UriBuilder
  39. {
  40. Scheme = "http",
  41. Host = _server.HostName,
  42. Port = _server.Port,
  43. Path = _server.VirtualPath
  44. }.ToString();
  45. }
  46. }
  47. /// <summary>
  48. /// Combine the RootUrl of the running web application with the relative url
  49. /// specified.
  50. /// </summary>
  51. /// <param name="relativeUrl"></param>
  52. /// <returns></returns>
  53. public string NormalizeUrl(string relativeUrl)
  54. {
  55. return CassiniNetworkUtils.NormalizeUrl(RootUrl, relativeUrl);
  56. }
  57. /// <summary>
  58. /// Will start specified application as "localhost" on loopback and first available port in the range 8000-10000 with vpath "/"
  59. /// </summary>
  60. /// <param name="applicationPath">Physical path to application.</param>
  61. /// <param name="virtualPath">Optional. defaults to "/"</param>
  62. public void StartServer(string applicationPath, string virtualPath)
  63. {
  64. StartServer(applicationPath, CassiniNetworkUtils.GetAvailablePort(8000, 10000, IPAddress.Loopback, true), virtualPath, "localhost");
  65. }
  66. /// <summary>
  67. /// Will start specified application as "localhost" on loopback and first available port in the range 8000-10000 with vpath "/"
  68. /// </summary>
  69. /// <param name="applicationPath">Physical path to application.</param>
  70. public void StartServer(string applicationPath)
  71. {
  72. StartServer(applicationPath, CassiniNetworkUtils.GetAvailablePort(8000, 10000, IPAddress.Loopback, true), "/", "localhost");
  73. }
  74. /// <summary>
  75. /// Will start specified application on loopback
  76. /// </summary>
  77. /// <param name="applicationPath">Physical path to application.</param>
  78. /// <param name="port">Port to listen on.</param>
  79. /// <param name="virtualPath">Optional. defaults to "/"</param>
  80. /// <param name="hostName">Optional. Is used to construct RootUrl. Defaults to "localhost"</param>
  81. public void StartServer(string applicationPath, int port, string virtualPath, string hostName)
  82. {
  83. // WebHost.Server will not run on any other IP
  84. IPAddress ipAddress = IPAddress.Loopback;
  85. if (!CassiniNetworkUtils.IsPortAvailable(ipAddress, port))
  86. {
  87. throw new Exception(string.Format("Port {0} is in use.", port));
  88. }
  89. applicationPath = Path.GetFullPath(applicationPath);
  90. virtualPath = String.Format("/{0}/", (virtualPath ?? string.Empty).Trim('/')).Replace("//", "/");
  91. hostName = string.IsNullOrEmpty(hostName) ? "localhost" : hostName;
  92. StartServer(applicationPath, ipAddress, port, virtualPath, hostName);
  93. }
  94. /// <summary>
  95. /// </summary>
  96. /// <param name="applicationPath">Physical path to application.</param>
  97. /// <param name="ipAddress">IP to listen on.</param>
  98. /// <param name="port">Port to listen on.</param>
  99. /// <param name="virtualPath">Optional. default value '/'</param>
  100. /// <param name="hostname">Optional. Used to construct RootUrl. Defaults to 'localhost'</param>
  101. public void StartServer(string applicationPath, IPAddress ipAddress, int port, string virtualPath, string hostname)
  102. {
  103. if (_server != null)
  104. {
  105. throw new InvalidOperationException("Server already started");
  106. }
  107. _server = new Server(port, virtualPath, applicationPath, ipAddress, hostname, 60000);
  108. try
  109. {
  110. _server.Start();
  111. }
  112. catch (Exception ex)
  113. {
  114. throw new InvalidOperationException("Error starting server instance.", ex);
  115. }
  116. }
  117. /// <summary>
  118. /// <para>Stops the server.</para>
  119. /// </summary>
  120. public void StopServer()
  121. {
  122. if (_server != null)
  123. {
  124. try
  125. {
  126. _server.ShutDown();
  127. }
  128. catch
  129. {
  130. }
  131. }
  132. }
  133. /// <summary>
  134. /// </summary>
  135. public void Dispose()
  136. {
  137. if (_server != null)
  138. {
  139. try
  140. {
  141. StopServer();
  142. _server.Dispose();
  143. _server = null;
  144. }
  145. catch
  146. {
  147. }
  148. }
  149. }
  150. }
  151. }