@Marc Gravell gave a great example of how to install a Windows service here . I went and implemented it, and everything was fine.
Then I rebooted my computer ... and suddenly I started getting security exceptions when trying to install! I get SecurityException: "Requested registry access is not allowed." I thought that maybe the problem started with a reboot, so like in cartoons, where a second blow to the head cures amnesia, I tried to restart again ... but it turns out that life is not like cartoons ...: (
OK, so I looked for this problem and found suggestions for granting rights to the registry key HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ services \ eventlog for my network service. That didn't work either. I gave all the rights to everyone - and HURRAY! Now I get another exception! InvalidOperationException: "Cannot open the service control manager on the computer." This operation may require other privileges. "(Internal exception Win32Exception:" Access denied. ") Um, I'm sorry? I'm trying to install on a local computer! What is a" computer "?" "What to do?"
This is very frustrating, because, as I said, yesterday it worked fine, and today everything fell apart, without any obvious changes in the code base.
Here is my code that performs the installation (copied and adapted from a sample of Mark Gravel):
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;
}
}
Installer Code:
[RunInstaller(true)]
public sealed class MyServiceInstallerProcess : ServiceProcessInstaller {
public MyServiceInstallerProcess() {
this.Account = ServiceAccount.NetworkService;
}
}
[RunInstaller(true)]
public sealed class MyServiceInstaller : ServiceInstaller {
public MyServiceInstaller() {
this.Description = "My service desc";
this.DisplayName = "My service name";
this.ServiceName = "My service name";
this.StartType = ServiceStartMode.Automatic;
}
}
What could be wrong here? And why did things suddenly start to go away after they worked perfectly well in advance?
source
share