Beispiel #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);
                }
            }
        }
Beispiel #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();
                }
            }
        }
Beispiel #3
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);
        }
Beispiel #4
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();
                }
            }
        }
Beispiel #5
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);
        }
Beispiel #6
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();
                }
            }
        }