C# - Windows 服务安装程序未注册服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1190294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:25:33  来源:igfitidea点击:

C# - windows service installer not registering service

c#windows-serviceswindows-installersetup-deployment

提问by David Hodgson

I'm trying to use an installer for a Windows service, and would like to avoid using InstallUtil.exe. The installer appears to work correctly (the executable and dlls are in the correct directory), but the service doesn't appear under Computer Management.

我正在尝试使用 Windows 服务的安装程序,并希望避免使用 InstallUtil.exe。安装程序似乎工作正常(可执行文件和 dll 位于正确的目录中),但该服务未出现在“计算机管理”下。

Here's what I've done so far:

这是我到目前为止所做的:

The service class name is the default - Service1.

服务类名称是默认的 - Service1。

In the Project installer, the ServiceName of the service installer matches the class name - Service1.

在项目安装程序中,服务安装程序的 ServiceName 与类名 - Service1 匹配。

Under the Custom Actions, the primary output of the service was added to Install, Commit, Rollback, and Uninstall.

在自定义操作下,服务的主要输出被添加到安装、提交、回滚和卸载。

I'm using http://support.microsoft.com/kb/816169as a reference.

我使用http://support.microsoft.com/kb/816169作为参考。

Any ideas?

有任何想法吗?

采纳答案by HasaniH

Does your service project have an Installer class? You should have one that looks something like this:

您的服务项目是否有安装程序类?你应该有一个看起来像这样的:

[RunInstaller(true)]
public partial class Service1Installer : Installer
{
    public Service1Installer()
    {
        InitializeComponent();
        ServiceProcessInstaller process = new ServiceProcessInstaller();
        process.Account = ServiceAccount.LocalSystem;

        ServiceInstaller serviceAdmin = new ServiceInstaller();
        serviceAdmin.StartType = ServiceStartMode.Manual;
        serviceAdmin.ServiceName = "Service1";
        serviceAdmin.DisplayName = "Service1";
        serviceAdmin.Description = "Service1";

        Installers.Add(serviceAdmin);
    }
}

回答by Robert Harvey

Make sure you've created a ServiceInstaller and ServiceProcessInstaller class in your service project. (Check this linkfor more info).

确保您已经在服务项目中创建了 ServiceInstaller 和 ServiceProcessInstaller 类。(查看此链接了解更多信息)。

Close computer management and the Services window, run your installer again, and reopen the Services window.

关闭计算机管理和服务窗口,再次运行安装程序,然后重新打开服务窗口。

If that doesn't work, restart your computer. You might have some files locked.

如果这不起作用,请重新启动计算机。您可能锁定了一些文件。

It goes without saying that you probably need administrative privileges on the machine for this to work properly.

不言而喻,您可能需要机器的管理权限才能正常工作。

回答by David Hodgson

I think I've figured it out. It might be a bug with the Designer code, or perhaps I missed a step.

我想我已经想通了。这可能是 Designer 代码的错误,或者我错过了一个步骤。

I think in the designer code, in the InitializeComponent() method, it's supposed to add:

我认为在设计器代码中,在 InitializeComponent() 方法中,应该添加:

this.Installers.AddRange(new System.Configuration.Install.Installer[] {this.serviceProcessInstaller1, this.serviceInstaller1});

It wasn't there, so I added this in the ProjectInstaller constructor:

它不存在,所以我在 ProjectInstaller 构造函数中添加了它:

Installers.Add(serviceInstaller1);
Installers.Add(serviceProcessInstaller1);

Now on installation, it's listed as a service in Computer Management.

现在在安装时,它被列为计算机管理中的一项服务。