我有一个应用程序想作为 Windows 服务 运行。按照 this MSDN doc page 中提供的说明进行操作(我需要托管一个WCF服务,所以过程中也详细介绍了这部分),我可以做到,如果我实现示例服务就可以了。我使用 installutil.exe
实用程序,可以将我的应用程序作为 Windows 服务安装和卸载。
我的问题
但是我需要在我的本地机器上安装相同应用程序的更多服务。所以我需要给他们不同的 System.ServiceProcess.ServiceBase.ServiceName
!所以再考虑一下安装代码:
[RunInstaller(true)]
public class ProjectInstaller : Installer {
private ServiceProcessInstaller process;
private ServiceInstaller service;
public ProjectInstaller() {
process = new ServiceProcessInstaller();
process.Account = ServiceAccount.LocalSystem;
service = new ServiceInstaller();
service.ServiceName = /* NEED TO PUT HERE THE NAME!!! */;
Installers.Add(process);
Installers.Add(service);
}
}
在调用 installutil.exe
时,有没有办法让我传递服务的名称?
如何解决这个问题?我还尝试使用 App.Config
文件并执行如下操作:
public ProjectInstaller() {
process = new ServiceProcessInstaller();
process.Account = ServiceAccount.LocalSystem;
service = new ServiceInstaller();
service.ServiceName = System.Configuration.ConfigurationManager.
AppSettings["SrvName"];
Installers.Add(process);
Installers.Add(service);
}
但是当然没用,这个文件在应用程序运行的时候被调用!!!
最佳答案
您可以打开配置文件来执行程序集。如果您的安装程序代码放在主服务 exe 文件中 - 它将是您的 app.config。否则,配置文件需要命名为 [assemblyname].dll.config。
process = new ServiceProcessInstaller();
process.Account = ServiceAccount.LocalSystem;
service = new ServiceInstaller();
var path = Assembly.GetExecutingAssembly().Location;
var config = ConfigurationManager.OpenExeConfiguration(path);
service.ServiceName = config.AppSettings.Settings["ServiceName"];
Installers.Add(process);
Installers.Add(service);
此外,本文还介绍了如何 pass installutil parameters explicitly through command line .
关于c# - 通过 `Configuration.Install.Installer` 安装长期运行的服务时如何将参数传递给 `installutil`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16768840/