Installing a Windows Service with dependencies

后端 未结 5 916
小蘑菇
小蘑菇 2020-12-15 21:18

My installer program doesn\'t suppport installing services but I can run a program/command line etc so my question is how can I install a Windows Service and add 2 dependenc

相关标签:
5条回答
  • 2020-12-15 21:39

    One method that's available is sc.exe. It allows you to install and control services from a command prompt. Here is an older article covering it's use. It does allow you to specify dependencies as well.

    Take a look at the article for the sc create portion for what you need.

    0 讨论(0)
  • 2020-12-15 21:44

    You can write a self-installing service and have it set a list of services your service depends on when the installer is executed.

    Basic steps:

    • Add a reference to System.Configuration.Install to your project.
    • Add a class that derives from System.Configuration.Install.Installer and has the RunInstaller attribute applied.
    • In its constructor create both a ServiceProcessInstaller and a ServiceInstaller object.
    • On the ServiceInstaller object you mark all the dependencies you want/need with the ServicesDependedOn property.
    • Add these two installers to the InstallersCollection your installer inherited from System.Configuration.Install.Installer
    • done.

    edit: forgot to mention that you can use e.g. Installutil.exe to invoke the installer.

    [RunInstaller(true)]
    public class MyServiceInstaller : Installer
    {
        public MyServiceInstaller()
        {
            using ( ServiceProcessInstaller procInstaller=new ServiceProcessInstaller() ) {
                procInstaller.Account = ServiceAccount.LocalSystem;
                using ( ServiceInstaller installer=new ServiceInstaller() ) {
                    installer.StartType = ServiceStartMode.Automatic;
                    installer.ServiceName = "FooService";
                    installer.DisplayName = "serves a lot of foo.";
    
                    installer.ServicesDependedOn = new string [] { "CLIPBOOK" };
                    this.Installers.Add(procInstaller);
                    this.Installers.Add(installer);
                }
            }
        }
    }
    0 讨论(0)
  • 2020-12-15 21:44

    There is a dynamic installer project on codeproject that I have found useful for services installation, in general.

    0 讨论(0)
  • 2020-12-15 21:45

    This can also be done via an elevated command prompt using the sc command. The syntax is:

    sc config [service name] depend= <Dependencies(separated by / (forward slash))>
    

    Note: There is a space after the equals sign, and there is not one before it.

    Warning: depend= parameter will overwrite existing dependencies list, not append. So for example, if ServiceA already depends on ServiceB and ServiceC, if you run depend= ServiceD, ServiceA will now depend only on ServiceD.

    Examples

    Dependency on one other service:

    sc config ServiceA depend= ServiceB
    

    Above means that ServiceA will not start until ServiceB has started. If you stop ServiceB, ServiceA will stop automatically.

    Dependency on multiple other services:

    sc config ServiceA depend= ServiceB/ServiceC/ServiceD
    

    Above means that ServiceA will not start until ServiceB, ServiceC, and ServiceD have all started. If you stop any of ServiceB, ServiceC, or ServiceD, ServiceA will stop automatically.

    To remove all dependencies:

    sc config ServiceA depend= /
    

    To list current dependencies:

    sc qc ServiceA
    
    0 讨论(0)
  • 2020-12-15 21:47

    Visual Studio Setup/Deployment projects work for this. They are not the best installer engine, but they work fine for simple scenarios.

    0 讨论(0)
提交回复
热议问题