views:

33

answers:

1

Don't know if there is a better way to do this, so that is the reason for the question. I can check if a service exists on a particular machine with the following code:

bool DoesServiceExist(string serviceName, string machineName)
{
    ServiceController controller = null;
    try
    {
        controller = new ServiceController(serviceName, machineName);
        controller.Status;
        return true;
    }
    catch(InvalidOperationException)
    {
        return false;
    }
    finally
    {
         if (controller != null)
         {
             controller.Dispose();
         }
    }
}

but this seems like an ineffecient solution to me (due to the exception handling). Is there a better way to check if a service exists. Note - I have recently switched to .Net 4.0 so if someone knows of a better solution in 4.0 that would be acceptable.

EDIT: Here is a sample c# console app to test the performance of my example as well as the GetServices code sample. In my testing I found that the GetServices is much more performative in the case where the service does not exist, but is twice as slow when the service does exist:

    static void Main(string[] args)
    {
        string serviceName = string.Empty;
        string machineName = string.Empty;

        var sw = new Stopwatch();
        sw.Reset();
        sw.Start();
        for (int i = 0; i < 1000; i++)
        {
            ServiceExistsException(serviceName, machineName);
        }
        sw.Stop();
        Console.WriteLine("Elapsed time: " + sw.ElapsedMilliseconds.ToString());
        sw.Reset();
        sw.Start();
        for (int i = 0; i < 1000; i++)
        {
            ServiceExistsGetList(serviceName, machineName);
        }
        sw.Stop();
        Console.WriteLine("Elapsed time: " + sw.ElapsedMilliseconds.ToString());

        Console.WriteLine("Done");
        Console.ReadLine();
    }

    static bool ServiceExistsException(string serviceName, string machineName)
    {
        ServiceController controller = null;
        try
        {
            controller = new ServiceController(serviceName, machineName);
            string name = controller.DisplayName;
            return true;
        }
        catch (InvalidOperationException)
        {
            return false;
        }
        finally
        {
            if (controller != null)
            {
                controller.Dispose();
            }
        }
    }

    static bool ServiceExistsGetList(string serviceName, string machineName)
    {
        ServiceController[] services = null;
        try
        {
            services = ServiceController.GetServices(machineName);
            var service = services.FirstOrDefault(s => s.ServiceName == serviceName);
            return service != null;
        }
        finally
        {
            if (services != null)
            {
                foreach (ServiceController controller in services)
                {
                    controller.Dispose();
                }
            }
        }
    }
}
+3  A: 

You can use the ServiceController.GetServices() method to get all of the services on the machine, then look through them to see if one exists named what you are looking for:

bool DoesServiceExist(string serviceName, string machineName)
{
    ServiceController[] services = ServiceController.GetServices(machineName);
    var service = services.FirstOrDefault(s => s.ServiceName == serviceName);
    return service != null;
}

The FirstOrDefault() extension method will return either the first service with the given name, or a null if there is no match.


To address you speed issue:

The difference between the two approaches for a single method call is negligible, regardless of whether the service is found or not. It will only be a problem if you are calling this method thousands of times - in which case get the list of services once and remember it.

adrianbanks
Pretty concise! +1
Nayan
I was questionable on the performance of your solution, so I ran a test. Your solution is far more performative (even with the code added to dispose of the service controllers) when the service does not exist. In the case where the service does exist this solution is twice as slow. I am not sure what to do now. In my case I am expecting that more often than not the service will exist. In that case my solution wins out, in the more generic case your solution wins out.
pstrjds
One additional comment. Basically this solution runs in constant time whether the service exists or not. Kudos for a good generic case solution.
pstrjds
In response to the speed issue, the code I presented in the question is faster when the service does exist (although I agree that it is negligible). I would not normally be calling this in a loop (just did that for performance analysis), otherwise I could cache the list. I think the hit is in iterating the list twice. Once to find the service (although not necessarily a complete iteration of the whole list) and another time to dispose of the objects. In the case where I am implementing I cannot cache the list, I may run the function days apart in which case the service could have been removed.
pstrjds