예제 #1
0
        private void Stop()
        {
            // kill the start button so we don't get a start
            // signal before completely stopped.
            ButtonStart.Enabled = false;

            // revert the host file modification, if necessary
            if (AddHost && RunState == RunState.Running)
            {
                HostsFile.RemoveHostEntry(_server.IPAddress.ToString(), _server.HostName);
            }

            if (_server != null)
            {
                _server.TimedOut -= OnTimedOut;
                _server.Dispose();
            }

            RootUrl = string.Empty;

            InvokeSetRunState(RunState.Idle);

            if (_automated)
            {
                ExitApp();
            }
        }
예제 #2
0
 private static void SetHostsFile(CommandLineArguments sargs)
 {
     try
     {
         if (sargs.AddHost)
         {
             HostsFile.AddHostEntry(sargs.IPAddress, sargs.HostName);
         }
         else
         {
             HostsFile.RemoveHostEntry(sargs.IPAddress, sargs.HostName);
         }
     }
     catch (UnauthorizedAccessException)
     {
         Environment.Exit(-1);
     }
     catch
     {
         Environment.Exit(-2);
     }
 }
예제 #3
0
        private static void Main(string[] cmdLine)
        {
            CommandLineArguments args = new CommandLineArguments();


            if (!CommandLineParser.ParseArgumentsWithUsage(cmdLine, args))
            {
                Environment.Exit(-1);
            }
            else
            {
                switch (args.RunMode)
                {
                case RunMode.Server:
                    IPAddress ip = IPAddress.Loopback;
                    try
                    {
                        args.Validate();

                        ip = CommandLineArguments.ParseIP(args.IPMode, args.IPv6, args.IPAddress);
                        int port = args.PortMode == PortMode.FirstAvailable ?
                                   CassiniNetworkUtils.GetAvailablePort(args.PortRangeStart, args.PortRangeEnd, ip, true) :
                                   args.Port;

                        if (args.AddHost)
                        {
                            HostsFile.AddHostEntry(ip.ToString(), args.HostName);
                        }

                        using (var server =
                                   new Server(port, args.VirtualPath, args.ApplicationPath,
                                              ip, args.HostName, args.TimeOut))
                        {
                            server.Start();
                            Console.WriteLine("started: {0}\r\nPress Enter key to exit....", server.RootUrl);
                            Console.ReadLine();
                            server.ShutDown();
                        }
                    }
                    catch (CassiniException ex)
                    {
                        Console.WriteLine("error:{0} {1}",
                                          ex.Field == ErrorField.None
                                                  ? ex.GetType().Name
                                                  : ex.Field.ToString(), ex.Message);
                    }
                    catch (Exception ex2)
                    {
                        Console.WriteLine("error:{0}", ex2.Message);
                        Console.WriteLine(CommandLineParser.ArgumentsUsage(typeof(CommandLineArguments)));
                    }
                    finally
                    {
                        if (args.AddHost)
                        {
                            HostsFile.RemoveHostEntry(ip.ToString(), args.HostName);
                        }
                    }
                    break;

                case RunMode.Hostsfile:
                    SetHostsFile(args);
                    break;
                }
            }
        }