コード例 #1
0
ファイル: Shell.cs プロジェクト: gabbyhershey/PeanutButter
        private int InstallMe(CommandlineOptions cli)
        {
            var myExePath       = new FileInfo(Environment.GetCommandLineArgs()[0]).FullName;
            var existingSvcUtil = new WindowsServiceUtil(ServiceName);

            if (existingSvcUtil.Commandline == myExePath)
            {
                Console.WriteLine("Service already installed correctly");
                return((int)CommandlineOptions.ExitCodes.Success);
            }

            try
            {
                if (existingSvcUtil.IsInstalled)
                {
                    existingSvcUtil.Uninstall(true);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Service already installed at: " + existingSvcUtil.Commandline +
                                  " and I can't uninstall it: " + ex.Message);
                return((int)CommandlineOptions.ExitCodes.InstallFailed);
            }

            var svcUtil = new WindowsServiceUtil(ServiceName, DisplayName, myExePath);

            try
            {
                var bootFlag = ResolveBootFlagFor(cli);
                svcUtil.Install(bootFlag);
                Console.WriteLine("Installed!");
                return((int)CommandlineOptions.ExitCodes.Success);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unable to install: " + ex.Message);
                return((int)CommandlineOptions.ExitCodes.InstallFailed);
            }
        }
コード例 #2
0
        public void ServiceWithArgs()
        {
            // Arrange
            var myPath = new Uri(GetType().Assembly.Location).LocalPath;
            var myDir = Path.GetDirectoryName(myPath);
            var serviceExe = Path.Combine(myDir, "ServiceWithArgs.exe");
            var logFile = Path.Combine(myDir, "service.log");
            var arg1 = GetRandomString(3);
            var arg2 = GetRandomString(3);
            var args = new[] { logFile, arg1, arg2 }.Select(p => p.QuoteIfSpaced());
            var serviceName = "test-service-foo-bar";
            var displayName = "Test Service - Foo,Bar";
            var commandline = string.Join(
                " ",
                new[] { serviceExe }
                .Concat(args)
                );
            var util = new WindowsServiceUtil(
                serviceName,
                displayName,
                commandline
                );

            if (util.IsInstalled)
            {
                util.Uninstall();
            }

            if (File.Exists(logFile))
            {
                File.Delete(logFile);
            }

            // Act
            using var resetter = new AutoResetter(
                      () => util.Install(),
                      () => util.Uninstall(true));
            util.Install();
            // Assert
            Expect(util.IsInstalled)
            .To.Be.True();
            util.Start();
            Expect(util.State)
            .To.Equal(ServiceState.Running);

            Expect(logFile)
            .To.Exist();
            var logData = TryReadAllLinesFrom(logFile);

            Expect(logData)
            .Not.To.Be.Empty();
            Expect(logData[0])
            .To.Contain(arg1)
            .Then(arg2);

            util.Stop();

            Expect(util.State)
            .To.Equal(ServiceState.Stopped);

            var anotherUtil = Create(serviceName);

            Expect(anotherUtil.Commandline)
            .To.Equal(commandline);
            Expect(anotherUtil.ServiceExe)
            .To.Equal(serviceExe);
            Expect(anotherUtil.DisplayName)
            .To.Equal(displayName);
        }