C# 为什么组件设计器会生成它(似乎)不使用的服务安装程序对象?
按照中详细介绍的步骤,我创建了一个已成功安装和测试的Windows服务。但我对用于安装服务的组件中的某些内容感到困惑 服务的安装由设计器生成的C# 为什么组件设计器会生成它(似乎)不使用的服务安装程序对象?,c#,windows,visual-studio-2010,service,installation,C#,Windows,Visual Studio 2010,Service,Installation,按照中详细介绍的步骤,我创建了一个已成功安装和测试的Windows服务。但我对用于安装服务的组件中的某些内容感到困惑 服务的安装由设计器生成的ProjectInstaller类完成,如下所示。此类由installutil.exe(在.NET Framework中)用于安装服务及其关联的服务进程。设计器生成的代码为服务创建了两个安装程序对象:MyServiceInstaller,为服务进程创建了myserviceceprocessinstaller,供安装时installutil.exe使用。但是
ProjectInstaller
类完成,如下所示。此类由installutil.exe
(在.NET Framework中)用于安装服务及其关联的服务进程。设计器生成的代码为服务创建了两个安装程序对象:MyServiceInstaller
,为服务进程创建了myserviceceprocessinstaller
,供安装时installutil.exe
使用。但是,它只将后者插入到安装程序集合中
那么,如何安装服务本身呢?如果服务流程中没有明确安装服务,那么服务流程是否有“默认服务”
代码:
(如果您对此进行实验,您会发现设计器实际上使用两个分部类创建了这个类。为了简单起见,我将它们合并为一个类。)
namespace-MyService
{
[运行安装程序(true)]
公共类ProjectInstaller:System.Configuration.Install.Installer
{
公共项目安装程序()
{
初始化组件();
}
///
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
///
私有void InitializeComponent()
{
this.MyServiceProcessInstaller=new System.ServiceProcess.ServiceProcessInstaller();
this.MyServiceInstaller=new System.ServiceProcess.ServiceInstaller();
//
//MyServiceProcessInstaller
//
this.MyServiceProcessInstaller.Account=System.ServiceProcess.ServiceAccount.LocalSystem;
this.MyServiceProcessInstaller.Password=null;
this.MyServiceProcessInstaller.Username=null;
//
//MyServiceInstaller
//
this.MyServiceInstaller.ServiceName=“MyService”;
//
//投射器
//
this.Installers.AddRange(新的System.Configuration.Install.Installer[]{
//***此处应为:this.MyServiceInstaller,
this.MyServiceProcessInstaller});
}
private System.ServiceProcess.ServiceProcessInstaller MyServiceProcessInstaller;
private System.ServiceProcess.ServiceInstaller MyServiceInstaller;
}
}
确实需要将服务安装程序添加到安装程序集合中。我遵循了文章中描述的相同步骤,结果正确无误:
private void InitializeComponent()
{
this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
//
// serviceInstaller1
//
this.serviceInstaller1.ServiceName = "Service1";
//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.serviceInstaller1});
}
我模模糊糊地记得读到,其中一个用于installutil
,另一个用于构建Windows安装程序。不过,我可能错了。@Joe-谢谢,我会把这看作是可能的。这能解释吗@雅各布-谢谢。不幸的是,这篇文章让我更加困惑,因为它说(参见备注部分):“使用ServiceProcessInstaller和ServiceInstaller实例设置服务的安装属性,并将实例添加到Installers集合。”这正是我所期望的。但是,设计器生成的代码仅将ServiceProcessInstaller添加到Installers集合。这就是我的问题。对不起,回复太慢了。非常忙。作为练习,我重复了创建服务的指定步骤,并得到了预期的结果(即与您的相同)。然而,我知道当我最初这样做的时候,我肯定只得到了服务过程的安装程序,而不是服务本身,并且我没有破解设计器生成的代码。然后我安装了它,服务运行得很好。因此,我认为有些情况下,设计师的行为与我在问题中所描述的一样,但在这一点上,不值得再花费更多的时间。谢谢你调查这件事。
private void InitializeComponent()
{
this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
//
// serviceInstaller1
//
this.serviceInstaller1.ServiceName = "Service1";
//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.serviceInstaller1});
}