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

11

我正在尝试使用一个Windows服务安装程序,想要避免使用InstallUtil.exe。 安装程序似乎正常工作(可执行文件和dll在正确的目录中),但服务未出现在计算机管理中。

到目前为止,我已经完成了以下操作:

服务类名是默认的-Service1。

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

在自定义操作中,服务的主要输出被添加到Install、Commit、Rollback和Uninstall中。

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

有什么好的意见吗?

3个回答

16

你的服务项目是否有一个名为Installer的类?你应该创建一个像下面这样的类:

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

这就是我所缺少的。我以为Installers.Add()部分会包含在自动生成的设计师代码中,但实际上并没有。也许他们改变了吧? - David Hodgson
是的,我看到你在发布时已经弄清楚了。 - HasaniH

3

确保您在服务项目中创建了ServiceInstaller和ServiceProcessInstaller类。(有关更多信息,请参见此链接)。

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

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

毫无疑问,您可能需要机器上的管理员权限才能正常工作。


链接中的示例代码让我找到了正确的方向,非常感谢。 - David Hodgson

0

我想我已经弄清楚了。这可能是设计师代码的一个错误,或者我可能错过了某个步骤。

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

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

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

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

现在安装后,它将在计算机管理中列为服务。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接