C# – Windows服务安装程序没有注册服务

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

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

服务类名称是默认值 – Service1。

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

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

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

有任何想法吗?

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

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

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

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

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

毫无疑问,您可能需要在计算机上拥有管理权限才能使其正常工作。

我想我已经明白了。 它可能是Designer代码的错误,或者我错过了一步。

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

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

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

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

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