1

Context

Windows 2008 64 bit.
I have a .NET service installed that acts as an installer.

Background

I'm using this code (credit: Marc Gravell) to install a service:

using (var inst = new AssemblyInstaller(typeof(MyNamespace.Program).Assembly, new string[] { })) {
    IDictionary state = new Hashtable();
    inst.UseNewContext = true;
    try {
    if (uninstall) {
        inst.Uninstall(state);
    } else {
        inst.Install(state);
        inst.Commit(state);
    }
    } catch {
    try {
        inst.Rollback(state);
    } catch { }
    throw;
    }
}

The Problem

All works fine and no exceptions, but right after that, I try to run the following code to start the service just installed:

using (var sc = new ServiceController("the service's name"))
{
    sc.Start();
    sc.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(20));
}

And I get an exception:

System.InvalidOperationException: Service [service name goes here] was not found on computer '.'. ---> System.ComponentModel.Win32Exception: The specified service does not exist as an installed service
   --- End of inner exception stack trace ---
   at System.ServiceProcess.ServiceController.GenerateNames()
   at System.ServiceProcess.ServiceController.get_ServiceName()
   at System.ServiceProcess.ServiceController.Start(String[] args)
   at System.ServiceProcess.ServiceController.Start()
   at ... (my code details)

I don't understand why, because:

  1. The service's name is the exact one as in the ServiceInstaller (in the ServiceName property)
  2. The code is executed inside a different service, which runs under Local System account.
Ron Klein
  • 9,178
  • 9
  • 55
  • 88

2 Answers2

0

It might be that the service is stuck in a waiting state and not yet accomplished the full registration, so it can't be recognized as an installed service.
As you do set UseNewContext property - does the installation log file ("{Assembly name}.InstallLog") contain any meaningful information?
Also, to check if this is a permissions issue, can you try to verify service existence using

sc query <ServiceName>

from an administrative command window?

rkellerm
  • 5,362
  • 8
  • 58
  • 95
0

There is an installation log that might help. Look for the YouServiceName.InstallLog file in the service's folder.

Adding an Installer for the service could be enough (it was for me). You should:

  • Open the Service.cs file in the designer,
  • Right click it, and
  • Choose the menu-option "Add Installer".

It won't install right out of the box... you need to create the installer class first.

For reference, see also this question.

Community
  • 1
  • 1
mvbattan
  • 1
  • 3