我正在学习如何使用WCF处理工作中的项目,因此我尝试根据以前项目的代码设置服务。虽然我成功了,每当我尝试安装可执行文件(使用installutil)时,它都会提示我输入凭据。经过一番检查后,我注意到这种情况发生了,因为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 = "XYZService";
//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.serviceInstaller1});
虽然基于它的项目用components = new System.ComponentModel.Container();
取代所有这些行,但它有效,但它不再要求凭据了。
但有人可以解释为什么会这样吗?我的经理告诉我,他专门做了这个改变以避免凭证提示,但没有解释为什么这行代码可以解决这个问题。
如果相关,则ProjectInstaller
方法已修改为:
InitializeComponent();
ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();
ServiceInstaller serviceInstaller = new ServiceInstaller();
//# Service Account Information
serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
serviceProcessInstaller.Username = null;
serviceProcessInstaller.Password = null;
//# Service Information
serviceInstaller.DisplayName = "XYZ Server";
serviceInstaller.Description = "This service initializes the XYZ Server";
serviceInstaller.StartType = ServiceStartMode.Automatic;
//# This must be identical to the WindowsService.ServiceBase name
//# set in the constructor of WindowsService.cs
serviceInstaller.ServiceName = XYZService._serviceName;
this.Installers.Add(serviceProcessInstaller);
this.Installers.Add(serviceInstaller);