ConsoleProgram.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.Collections;
  14. using System.Net;
  15. namespace CassiniDev
  16. {
  17. public class Program
  18. {
  19. [STAThread]
  20. private static void Main(string[] cmdLine)
  21. {
  22. CommandLineArguments args = new CommandLineArguments();
  23. if (!CommandLineParser.ParseArgumentsWithUsage(cmdLine, args))
  24. {
  25. Environment.Exit(-1);
  26. }
  27. else
  28. {
  29. switch (args.RunMode)
  30. {
  31. case RunMode.Server:
  32. IPAddress ip=IPAddress.Loopback;
  33. try
  34. {
  35. args.Validate();
  36. ip = CommandLineArguments.ParseIP(args.IPMode, args.IPv6, args.IPAddress);
  37. int port = args.PortMode == PortMode.FirstAvailable ?
  38. CassiniNetworkUtils.GetAvailablePort(args.PortRangeStart, args.PortRangeEnd, ip, true) :
  39. args.Port;
  40. if(args.AddHost)
  41. {
  42. HostsFile.AddHostEntry(ip.ToString(), args.HostName);
  43. }
  44. using (var server =
  45. new Server(port, args.VirtualPath, args.ApplicationPath,
  46. ip, args.HostName, args.TimeOut))
  47. {
  48. server.Start();
  49. Console.WriteLine("started: {0}\r\nPress Enter key to exit....", server.RootUrl);
  50. Console.ReadLine();
  51. server.ShutDown();
  52. }
  53. }
  54. catch (CassiniException ex)
  55. {
  56. Console.WriteLine("error:{0} {1}",
  57. ex.Field == ErrorField.None
  58. ? ex.GetType().Name
  59. : ex.Field.ToString(), ex.Message);
  60. }
  61. catch (Exception ex2)
  62. {
  63. Console.WriteLine("error:{0}", ex2.Message);
  64. Console.WriteLine(CommandLineParser.ArgumentsUsage(typeof(CommandLineArguments)));
  65. }
  66. finally
  67. {
  68. if (args.AddHost)
  69. {
  70. HostsFile.RemoveHostEntry(ip.ToString(), args.HostName);
  71. }
  72. }
  73. break;
  74. case RunMode.Hostsfile:
  75. SetHostsFile(args);
  76. break;
  77. }
  78. }
  79. }
  80. private static void SetHostsFile(CommandLineArguments sargs)
  81. {
  82. try
  83. {
  84. if (sargs.AddHost)
  85. {
  86. HostsFile.AddHostEntry(sargs.IPAddress, sargs.HostName);
  87. }
  88. else
  89. {
  90. HostsFile.RemoveHostEntry(sargs.IPAddress, sargs.HostName);
  91. }
  92. }
  93. catch (UnauthorizedAccessException)
  94. {
  95. Environment.Exit(-1);
  96. }
  97. catch
  98. {
  99. Environment.Exit(-2);
  100. }
  101. }
  102. }
  103. //public sealed class ServiceUtil
  104. //{
  105. // static void Install(bool undo, string[] args)
  106. // {
  107. // try
  108. // {
  109. // Console.WriteLine(undo ? "uninstalling" : "installing");
  110. // using (AssemblyInstaller inst = new AssemblyInstaller(typeof(Program).Assembly, args))
  111. // {
  112. // IDictionary state = new Hashtable();
  113. // inst.UseNewContext = true;
  114. // try
  115. // {
  116. // if (undo)
  117. // {
  118. // inst.Uninstall(state);
  119. // }
  120. // else
  121. // {
  122. // inst.Install(state);
  123. // inst.Commit(state);
  124. // }
  125. // }
  126. // catch
  127. // {
  128. // try
  129. // {
  130. // inst.Rollback(state);
  131. // }
  132. // catch { }
  133. // throw;
  134. // }
  135. // }
  136. // }
  137. // catch (Exception ex)
  138. // {
  139. // Console.Error.WriteLine(ex.Message);
  140. // }
  141. // }
  142. //}
  143. }