0

I'm having some issues with finding a service running on my local machine, but only when I'm using the parameter string instead of a hardcoded string (which I added to debug the problem).

My method looks like this:

public bool CheckIfServiceIsRunning(string serviceName)
{
    try
    {
        var services = ServiceController.GetServices();
        var service = services.FirstOrDefault(s => s.DisplayName == serviceName);
        var test = services.FirstOrDefault(s => s.DisplayName == "MongoDB");
        if (service == null)
        {
            return false;
        }
        return service.Status.Equals(ServiceControllerStatus.Running);
    }
    catch (InvalidOperationException ex)
    {
        Log.Info(ex.Message);
        throw new InvalidOperationException(ex.Message);
    }
}

Results of running this code with parameter "MongoDB":

service = null

test = System.ServiceProcess.ServiceController object with the MongoDB service

Edit: Using the following comparison tells me the strings aren't equal:

if (string.Compare(serviceName, "MongoDB", StringComparison.Ordinal) == 0)
{
    Console.WriteLine("same string");
}
Community
  • 1
  • 1
Jeroen
  • 1,625
  • 3
  • 16
  • 28
  • have you tried with string.Compare() ? and eventually adding the string comparison invariant culture? It seems the same to me.. I'm now wondering what can be the difference.. [String.Compare Method (String, String, CultureInfo, CompareOptions)](https://msdn.microsoft.com/en-us/library/cc190529(v=vs.110).aspx) – rmjoia Sep 28 '17 at 07:49
  • When doing a serviceName.Equals() to see if it matches "MongoDB" (with invariant culture) it's not equal. – Jeroen Sep 28 '17 at 07:51
  • that is nice.. :D but if you just compare ingnoring the culture maybe will work? – rmjoia Sep 28 '17 at 07:52
  • also refer to [Difference between InvariantCulture and Ordinal string comparison](https://stackoverflow.com/questions/492799/difference-between-invariantculture-and-ordinal-string-comparison) This is how I usually compare strings just to be safe – rmjoia Sep 28 '17 at 07:54
  • I've updated my question with this comparison – Jeroen Sep 28 '17 at 07:59
  • as I mentioned, you can use the StringComparison.InvariantCulture – rmjoia Sep 28 '17 at 08:00
  • Using StringComparison.InvariantCulture gives me the same result unfortunately – Jeroen Sep 28 '17 at 08:02
  • dammit.. no ideas – rmjoia Sep 28 '17 at 08:02
  • 1
    Try using serviceName.Trim() maybe. There is something wrong with the parameter you parse, take a good look at what you are actually putting in the method. The method itself looks fine. Set a breakpoint inside a method and check what string method gets. – erexo Sep 28 '17 at 08:04
  • @Erexo Thanks for this suggestion, apparently there was a fking white space at the end. Now it works, fk me – Jeroen Sep 28 '17 at 08:11
  • Is it appropriate to copy my message to answer so OP can mark it as correct answer ? – erexo Sep 28 '17 at 08:12
  • I think that's allowed. I will mark the answer as correct – Jeroen Sep 28 '17 at 08:50

1 Answers1

2

The method itself looks fine, there is something wrong with the parameter you parse, take a good look at what you are actually putting in the method. Try using serviceName.Trim(), strings can get tricky about whitespace characters before and/or after.

You can also set a breakpoint inside a method and check what exactly that serviceName contains.

erexo
  • 503
  • 1
  • 6
  • 24
  • '-.- I can't believe that the problem was spaces, because.. that would be the first place were I would search.. – rmjoia Sep 28 '17 at 13:52