1

This is a Windows Service project using VS2015 and .NET Framework 4.5.

I'm trying to install my service through a post-build action and then start it automatically by using ServiceController.Start(). Here's my code that tries to start the service:

private void ProjectInstaller_Committed(object sender, InstallEventArgs e)
{
  using (var sw = new System.IO.StreamWriter(Console.OpenStandardOutput()))
  {
    using (ServiceController sc = new ServiceController(serviceInstaller1.ServiceName))
    {
      try
      {
        sw.Write("Starting service...");
        sc.Start();
        sc.WaitForStatus(ServiceControllerStatus.Running);
        sw.Write("Service status is now set to {0}.", sc.Status.ToString());
      }
      catch (InvalidOperationException)
      {
        sw.Write("Could not start the service.");
      }
    }
  }
}

The service installs just fine, but my ServiceController.WaitForStatus() call seemingly keeps waiting forever. I have tried calling it from Committed and AfterInstall events.

dotNET
  • 33,414
  • 24
  • 162
  • 251

1 Answers1

2

Figured it out finally. My Start() call was actually failing, which I didn't notice until I went to Event Viewer. The error message was something like:

Your process does not have access rights to this namespace

Googling for the error message brought up another SO post where the accepted answer did the trick for me. Hope this helps someone down the road.

Community
  • 1
  • 1
dotNET
  • 33,414
  • 24
  • 162
  • 251