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:
- The service's name is the exact one as in the
ServiceInstaller
(in theServiceName
property) - The code is executed inside a different service, which runs under Local System account.