Reputation: 38013
I've written a Windows Service that exposes a WCF service to a GUI installed on the same machine. When I run the GUI, if I can't connect to the service, I need to know if it's because the service app hasn't been installed yet, or if it's because the service is not running. If the former, I'll want to install it (as described here); if the latter, I'll want to start it up.
Question is: how do you detect if the service is installed, and then having detected that it's installed, how do you start it up?
Upvotes: 89
Views: 59340
Reputation: 93
private bool ServiceExists(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
var service = services.FirstOrDefault(s => string.Equals(s.ServiceName, serviceName, StringComparison.OrdinalIgnoreCase));
return service != null;
}
Upvotes: 2
Reputation: 771
I think this is the best answer for this question. There is no need to add extra processing to verify if the service exists, since it will throw an exception if it doesn't. You just need to catch it. You also do not need to close() the connecting if you wrap the entire method in using().
using (ServiceController sc = new ServiceController(ServiceName))
{
try
{
if (sc.Status != ServiceControllerStatus.Running)
{
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 10));
//service is now Started
}
else
//Service was already started
}
catch (System.ServiceProcess.TimeoutException)
{
//Service was stopped but could not restart (10 second timeout)
}
catch (InvalidOperationException)
{
//This Service does not exist
}
}
Upvotes: 4
Reputation: 7884
Actually looping like this:
foreach (ServiceController SC in ServiceController.GetServices())
may throw Access Denied exception if the account under which your application is running doesn't have rights to view service properties. On the other hand, you can safely do this even if no service with such name exist:
ServiceController SC = new ServiceController("AnyServiceName");
But accessing its properties if service doesn't exist will result in InvalidOperationException. So here's a safe way to check if a service is installed:
ServiceController SC = new ServiceController("MyServiceName");
bool ServiceIsInstalled = false;
try
{
// actually we need to try access ANY of service properties
// at least once to trigger an exception
// not neccessarily its name
string ServiceName = SC.DisplayName;
ServiceIsInstalled = true;
}
catch (InvalidOperationException) { }
finally
{
SC.Close();
}
Upvotes: 10
Reputation: 71
For non-linq, you can just iterate thru the array like this:
using System.ServiceProcess;
bool serviceExists = false
foreach (ServiceController sc in ServiceController.GetServices())
{
if (sc.ServiceName == "myServiceName")
{
//service is found
serviceExists = true;
break;
}
}
Upvotes: 2
Reputation: 81660
Use:
// add a reference to System.ServiceProcess.dll
using System.ServiceProcess;
// ...
ServiceController ctl = ServiceController.GetServices()
.FirstOrDefault(s => s.ServiceName == "myservice");
if(ctl==null)
Console.WriteLine("Not installed");
else
Console.WriteLine(ctl.Status);
Upvotes: 161
Reputation: 430
You could use the following as well..
using System.ServiceProcess;
...
var serviceExists = ServiceController.GetServices().Any(s => s.ServiceName == serviceName);
Upvotes: 43