简体   繁体   中英

Problem with installing Windows service using AssemblyInstaller

@Marc Gravell gave a great example of how to install a Windows Service here . I went and implemented it, and everything was good.

Then I rebooted my computer... and suddenly I started getting security exceptions when I tried to install! I get a SecurityException : "Requested registry access is not allowed". I thought maybe the trouble started from the reboot, so like in the cartoons where the second blow to the head cures the amnesia, I tried rebooting again... but it turns out life isn't like cartoons... :(

OK, so I googled the problem, and found suggestions to give rights on the registry key HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\services\\eventlog to my network service. That didn't work, either. I gave full rights to Everyone - and HURRAH! I'm now getting a different exception! InvalidOperationException : "Cannot open Service Control Manager on computer '.'. This operation might require other privileges." (Inner exception is Win32Exception : "Access is denied".) Um, excuse me? I'm trying to install on the local computer! What is "computer '.'" doing there?

This is intensely frustrating, because as I said, yesterday it was working fine, and today everything has fallen apart, without any apparent change to the codebase.

Here's my code that does the installation (copied and adapted from Marc Gravell's sample):

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 installer code is:

[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 going haywire, after they were working fine beforehand?

Run your installer as an administrator/with elevated privileges. Giving everyone access to system registry keys and stuff is VERY WRONG.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM