Exemple #1
0
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.AssemblyResolve += Resolver;
            Init();

            if (!IsAnAdministrator())
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("Error: ");
                Console.ResetColor();
                Console.Write("Servant needs to run as administrator to access IIS.");
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                Console.Write("Solution: ");
                Console.ResetColor();
                Console.Write("Right click Servant.Server.exe and select 'Run as administrator'.");
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
                return;
            }

            if (!IsIisInstalled())
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("Error: ");
                Console.ResetColor();
                Console.Write("IIS needs to be installed to use Servant.");
                Console.WriteLine();
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
                return;
            }

            var command = args.FirstOrDefault() ?? "";

            Configuration = TinyIoCContainer.Current.Resolve <ServantConfiguration>();

            if (Configuration.IsHttps())
            {
                if (!IsServantCertificateInstalled())
                {
                    InstallServantCertificate();
                }

                var servantPort = new Uri(Configuration.ServantUrl).Port;
                if (!CertificateHandler.IsCertificateBound(servantPort))
                {
                    CertificateHandler.AddCertificateBinding(servantPort);
                }
            }

            switch (command)
            {
            case "install":
                if (IsAlreadyInstalled())
                {
                    Console.WriteLine("Servant is already installed. Use /uninstall to uninstall.");
                    Console.ReadLine();
                    return;
                }
                const string servantServiceName = "Servant for IIS";

                var existingServantService = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == servantServiceName);
                if (existingServantService != null)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Servant is already running on this machine.");
                    Console.ReadLine();
                    return;
                }
                ManagedInstallerClass.InstallHelper(new[] { "/LogToConsole=false", Assembly.GetExecutingAssembly().Location });
                var startController = new ServiceController("Servant for IIS");
                startController.Start();
                StartBrowser();
                Console.WriteLine("OK: Servant was successfully installed. Please complete the installation from your browser on " + Configuration.ServantUrl);
                break;

            case "uninstall":
                Console.WriteLine();
                Console.WriteLine("Trying to uninstall the Servant service...");
                try
                {
                    ManagedInstallerClass.InstallHelper(new[] { "/u", "/LogToConsole=false", Assembly.GetExecutingAssembly().Location });
                    Console.WriteLine("The service was successfully uninstalled.");
                }
                catch (Exception)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("An error occurred while trying to uninstall Servant.");
                    Console.ResetColor();
                    Environment.Exit(1);
                }

                break;

            default:
                if (Environment.UserInteractive || (args != null && args.Length > 0))
                {
                    Console.WriteLine();
                    Console.WriteLine("Welcome to Servant for IIS.");
                    Console.WriteLine();

                    StartServant();
                    Console.WriteLine("You can now manage your server from " + Configuration.ServantUrl);
                    StartBrowser();
                    while (true)
                    {
                        Console.ReadLine();
                    }
                }

                ServiceBase.Run(new ServantService());
                break;
            }
        }