public void StartAndStopInstalledService() { string serviceName = "MyServer"; Assert.True(InstallerServices.IsInstalled(serviceName), "The service must be installed."); TimeSpan timeout = new TimeSpan(0, 0, 0, 1); // 1 sec ServiceCommands commands = new ServiceCommands(); ServiceControllerStatus runningStatus = ServiceControllerStatus.Running; ServiceControllerStatus stoppedStatus = ServiceControllerStatus.Stopped; using (ServiceController sc = new ServiceController(serviceName)) { // execute bool started = commands.Start(serviceName); // verify Assert.True(started, "The service was not started."); sc.WaitForStatus(runningStatus, timeout); Assert.Equal(runningStatus.ToString(), commands.GetStatus(serviceName)); // execute bool stopped = commands.Stop(serviceName); // verify Assert.True(stopped, "The service was not stopped."); sc.WaitForStatus(stoppedStatus, timeout); Assert.Equal(stoppedStatus.ToString(), commands.GetStatus(serviceName)); } }
private static void CheckStatus(ServiceCommands commands, string serviceName) { bool isInstalled = InstallerServices.IsInstalled(serviceName); if (isInstalled) { string status = commands.GetStatus(serviceName); Console.WriteLine("Status of service '{0}': {1}.", serviceName, status); } else { Console.WriteLine("Service '{0}' is not installed.", serviceName); } }
public void InstallNotAlreadyInstalledService() { string serviceName = "MyServer"; // assume the service is NOT installed or the test can't work Assert.False(InstallerServices.IsInstalled(serviceName), "The service must not be installed."); ServiceCommands commands = new ServiceCommands(); bool installed = commands.Install(serviceName); // verify that the service has been installed Assert.True(installed, "The service has not been installed."); Assert.True(InstallerServices.IsInstalled(serviceName), "The service is still not installed."); // installed service should be stopped by default string status = commands.GetStatus(serviceName); Assert.Equal(ServiceControllerStatus.Stopped.ToString(), status); }