安装具有依赖项的 Windows 服务

发布于 2024-07-25 14:47:06 字数 107 浏览 4 评论 0 原文

我的安装程序不支持安装服务,但我可以运行程序/命令行等,所以我的问题是如何安装 Windows 服务并使用命令行添加 2 个依赖项? 该程序是.Net 2.0 应用程序。

谢谢

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 dependencies using the command line? The program is a .Net 2.0 app.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

行雁书 2024-08-01 14:47:06

您可以编写一个自安装服务,并让它在执行安装程序时设置您的服务所依赖的服务列表。

基本步骤:

编辑:忘记提及您可以使用例如 Installutil.exe 来调用安装程序。

[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);
            }
        }
    }
}

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:

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);
            }
        }
    }
}
千仐 2024-08-01 14:47:06

这也可以使用 sc 命令通过提升的命令提示符来完成。 语法为:

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

注意:等号后有一个空格,且等号前没有一个空格。

警告depend= 参数将覆盖现有依赖项列表,而不是追加。 例如,如果 ServiceA 已经依赖于 ServiceB 和 ServiceC,那么如果您运行 depend= ServiceD,ServiceA 现在将依赖于 ServiceD。

示例

依赖于另一项服务:

sc config ServiceA depend= ServiceB

上面的意思是,ServiceA 在 ServiceB 启动之前不会启动。 如果停止ServiceB,ServiceA也会自动停止。

对多个其他服务的依赖:

sc config ServiceA depend= ServiceB/ServiceC/ServiceD

上面的意思是,在 ServiceB、ServiceC 和 ServiceD 全部启动之前,ServiceA 不会启动。 如果停止ServiceB、ServiceC、ServiceD中的任意一个,ServiceA将自动停止。

删除所有依赖项:

sc config ServiceA depend= /

列出当前依赖项:

sc qc ServiceA

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
只是我以为 2024-08-01 14:47:06

一种可用的方法是 sc.exe。 它允许您从命令提示符安装和控制服务。 这是一篇旧文章,介绍了它的用法。 它也允许您指定依赖项。

请参阅文章中的 sc create 部分来了解您的需要。

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.

送君千里 2024-08-01 14:47:06

我发现 codeproject 上有一个动态安装程序项目,一般来说对于服务安装很有用。

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

寄风 2024-08-01 14:47:06

Visual Studio 安装/部署项目适用于此。 它们不是最好的安装程序引擎,但对于简单的场景来说它们工作得很好。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文