C# 安装在Visual Studio中创建的Windows服务
当我在VisualStudio2010中创建一个新的Windows服务时,我收到一条消息,说明要使用InstallUtil和net start来运行该服务 我尝试了以下步骤:C# 安装在Visual Studio中创建的Windows服务,c#,windows,service,windows-services,installutil,C#,Windows,Service,Windows Services,Installutil,当我在VisualStudio2010中创建一个新的Windows服务时,我收到一条消息,说明要使用InstallUtil和net start来运行该服务 我尝试了以下步骤: 创建新项目文件->新建->项目->Windows服务 项目名称:TestService 按原样构建项目(Service1构造函数、OnStart、OnStop) 打开命令提示符,运行“C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe”TestServ
Installer
继承的类,它将告诉installutil
如何将可执行文件作为服务安装
另外,我在这里有自己的小的自安装/可调试Windows服务模板,您可以从中复制代码或使用它:您需要在设计器中打开Service.cs文件,右键单击它并选择菜单选项“添加安装程序” 它不会直接安装在盒子里。。。您需要首先创建安装程序类 有关服务安装程序的一些参考: 很老了。。。但这就是我所说的: 通过执行此操作,将自动创建
ProjectInstaller.cs
。然后,您可以双击该按钮,进入设计器并配置组件:
具有服务本身的属性:serviceInstaller1
、Description
、DisplayName
和ServiceName
是最重要的StartType
具有以下重要属性:serviceProcessInstaller1
,即运行服务的帐户Account
this.serviceProcessInstaller1.Account = ServiceAccount.LocalSystem;
两个典型问题:
ProjectInstaller
类是public
。老实说,我不确定我到底是如何做到的,但我将事件处理程序添加到了ProjectInstaller.Designer.cs
,如:
this.serviceProcessInstaller1.BeforeInstall+=new System.Configuration.Install.InstallEventHandler(this.serviceProcessInstaller1\u BeforeInstall)代码>
我猜在ProjectInstaller.cs
中自动创建处理程序函数的过程中,它更改了
public类ProjectInstaller:System.Configuration.Install.Installer
到
分部类ProjectInstaller:System.Configuration.Install.Installer
将public
关键字替换为partial
。所以,为了修复它,必须
public分部类ProjectInstaller:System.Configuration.Install.Installer
using System.Configuration.Install;
using System.ServiceProcess;
using System.ComponentModel;
namespace SAS
{
[RunInstaller(true)]
public class MyProjectInstaller : Installer
{
private ServiceInstaller serviceInstaller1;
private ServiceProcessInstaller processInstaller;
public MyProjectInstaller()
{
// Instantiate installer for process and service.
processInstaller = new ServiceProcessInstaller();
serviceInstaller1 = new ServiceInstaller();
// The service runs under the system account.
processInstaller.Account = ServiceAccount.LocalSystem;
// The service is started manually.
serviceInstaller1.StartType = ServiceStartMode.Manual;
// ServiceName must equal those on ServiceBase derived classes.
serviceInstaller1.ServiceName = "SAS Service";
// Add installer to collection. Order is not important if more than one service.
Installers.Add(serviceInstaller1);
Installers.Add(processInstaller);
}
}
}
RunInstaller(true) // old alias
RunInstallerAttribute(true) // new property spelling