Example #1
0
        // [Fact]
        // To cleanup lingering Test Services uncomment the Fact attribute, make it public and run the following command
        // dotnet build /t:test /p:XunitMethodName=System.ServiceProcess.Tests.ServiceBaseTests.Cleanup /p:OuterLoop=true
        // Remember to comment out the Fact again before running tests otherwise it will cleanup tests running in parallel
        // and cause them to fail.
        private void Cleanup()
        {
            string currentService = "";

            foreach (ServiceController controller in ServiceController.GetServices())
            {
                try
                {
                    currentService = controller.DisplayName;
                    if (controller.DisplayName.StartsWith("Test Service"))
                    {
                        Console.WriteLine("Trying to clean-up " + currentService);
                        TestServiceInstaller deleteService = new TestServiceInstaller()
                        {
                            ServiceName = controller.ServiceName
                        };
                        deleteService.RemoveService();
                        Console.WriteLine("Cleaned up " + currentService);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed " + ex.Message);
                }
            }
        }
Example #2
0
        public void DeleteTestServices()
        {
            try
            {
                if (_client != null)
                {
                    DebugTrace("TestServiceProvider: Disposing client stream in Dispose()");
                    _client.Dispose();
                    _client = null;
                }

                TestServiceInstaller testServiceInstaller = new TestServiceInstaller();
                testServiceInstaller.ServiceName = TestServiceName;
                testServiceInstaller.RemoveService();
            }
            finally
            {
                // Lets be sure to try and clean up dependenct services even if something goes
                // wrong with the full removal of the other service.
                if (_dependentServices != null)
                {
                    _dependentServices.DeleteTestServices();
                }
            }
        }
Example #3
0
        private void CreateTestServices()
        {
            TestServiceInstaller testServiceInstaller = new TestServiceInstaller();

            testServiceInstaller.ServiceName = TestServiceName;
            testServiceInstaller.DisplayName = TestServiceDisplayName;
            testServiceInstaller.Description = "__Dummy Test Service__";
            testServiceInstaller.Username    = null;

            if (_dependentServices != null)
            {
                testServiceInstaller.ServicesDependedOn = new string[] { _dependentServices.TestServiceName };
            }

            var    comparer       = PlatformDetection.IsNetFramework ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal; // .NET Framework upper cases the name
            string processName    = Process.GetCurrentProcess().MainModule.FileName;
            string entryPointName = typeof(TestService).Assembly.Location;
            string arguments      = TestServiceName;

            // if process and entry point aren't the same then we are running hosted so pass
            // in the entrypoint as the first argument
            if (!PlatformDetection.IsNetFramework)
            {
                arguments = $"\"{entryPointName}\" {arguments}";
            }
            else
            {
                processName = entryPointName;
            }

            testServiceInstaller.ServiceCommandLine = $"\"{processName}\" {arguments}";

            testServiceInstaller.Install();
        }
Example #4
0
        static int Main(string[] args)
        {
            if (args.Length == 1 || args.Length == 2)
            {
                TestService testService = new TestService(args[0]);
                ServiceBase.Run(testService);
                return(0);
            }
            else if (args.Length == 3)
            {
                TestServiceInstaller testServiceInstaller = new TestServiceInstaller();

                testServiceInstaller.ServiceName = args[0];
                testServiceInstaller.DisplayName = args[1];

                if (args[2] == "create")
                {
                    testServiceInstaller.Install();
                    return(0);
                }
                else if (args[2] == "delete")
                {
                    testServiceInstaller.RemoveService();
                    return(0);
                }
                else
                {
                    Console.WriteLine("EROOR: Invalid Service verb. Only suppot create or delete.");
                    return(2);
                }
            }

            Console.WriteLine($"usage: <ServiceName> <DisplayName> [create|delete]");
            return(1);
        }
Example #5
0
        public void DeleteTestServices()
        {
            try
            {
                TestServiceInstaller testServiceInstaller = new TestServiceInstaller();
                testServiceInstaller.ServiceName = TestServiceName;
                testServiceInstaller.RemoveService();

                if (File.Exists(LogPath))
                {
                    try
                    {
                        File.Delete(LogPath);
                    }
                    catch (IOException)
                    {
                        // Don't fail simply because the service was not fully cleaned up
                        // and is still holding a handle to the log file
                    }
                }
            }
            finally
            {
                // Lets be sure to try and clean up dependenct services even if something goes
                // wrong with the full removal of the other service.
                if (_dependentServices != null)
                {
                    _dependentServices.DeleteTestServices();
                }
            }
        }
Example #6
0
        static int Main(string[] args)
        {
            if (args.Length == 1 || args.Length == 2)
            {
                TestService testService;
                if (args[0].StartsWith("PropagateExceptionFromOnStart"))
                {
                    var expectedException = new InvalidOperationException("Fail on startup.");
                    testService = new TestService(args[0], expectedException);
                    try
                    {
                        ServiceBase.Run(testService);
                    }
                    catch (Exception actualException)
                    {
                        if (object.ReferenceEquals(expectedException, actualException))
                        {
                            testService.WriteStreamAsync(PipeMessageByteCode.ExceptionThrown).Wait();
                        }
                        else
                        {
                            throw actualException;
                        }
                    }
                }
                else
                {
                    testService = new TestService(args[0]);
                    ServiceBase.Run(testService);
                }
                return(0);
            }
            else if (args.Length == 3)
            {
                TestServiceInstaller testServiceInstaller = new TestServiceInstaller();

                testServiceInstaller.ServiceName = args[0];
                testServiceInstaller.DisplayName = args[1];

                if (args[2] == "create")
                {
                    testServiceInstaller.Install();
                    return(0);
                }
                else if (args[2] == "delete")
                {
                    testServiceInstaller.RemoveService();
                    return(0);
                }
                else
                {
                    Console.WriteLine("EROOR: Invalid Service verb. Only suppot create or delete.");
                    return(2);
                }
            }

            Console.WriteLine($"usage: <ServiceName> <DisplayName> [create|delete]");
            return(1);
        }
Example #7
0
        public void DeleteTestServices()
        {
            try
            {
                TestServiceInstaller testServiceInstaller = new TestServiceInstaller();
                testServiceInstaller.ServiceName = TestServiceName;
                testServiceInstaller.RemoveService();

                if (File.Exists(LogPath))
                {
                    File.Delete(LogPath);
                }
            }
            finally
            {
                // Lets be sure to try and clean up dependenct services even if something goes
                // wrong with the full removal of the other service.
                if (_dependentServices != null)
                {
                    _dependentServices.DeleteTestServices();
                }
            }
        }
Example #8
0
        private void CreateTestServices()
        {
            DebugTrace($"TestServiceProvider: Creating test service {TestServiceName}");
            TestServiceInstaller testServiceInstaller = new TestServiceInstaller();

            testServiceInstaller.ServiceName = TestServiceName;
            testServiceInstaller.DisplayName = TestServiceDisplayName;
            testServiceInstaller.Description = "__Dummy Test Service__";
            testServiceInstaller.Username    = null;

            if (_prerequisiteServices != null)
            {
                DebugTrace($"TestServiceProvider: .. with prequisite services {_prerequisiteServices.TestServiceName}");
                testServiceInstaller.ServicesDependedOn = new string[] { _prerequisiteServices.TestServiceName };
            }

            string processName    = Process.GetCurrentProcess().MainModule.FileName;
            string entryPointName = typeof(TestService).Assembly.Location;
            string arguments      = TestServiceName;

            // if process and entry point aren't the same then we are running hosted so pass
            // in the entrypoint as the first argument
            if (!PlatformDetection.IsNetFramework)
            {
                arguments = $"\"{entryPointName}\" {arguments}";
            }
            else
            {
                processName = entryPointName;
            }

            testServiceInstaller.ServiceCommandLine = $"\"{processName}\" {arguments}";
            DebugTrace($"TestServiceProvider: Executing {testServiceInstaller.ServiceCommandLine}");
            testServiceInstaller.Install();
            DebugTrace("TestServiceProvider: Completed install of test service");
        }