c# - 通过 `Configuration.Install.Installer` 安装长期运行的服务时如何将参数传递给 `installutil`

标签 c# .net wcf windows-services windows-installer

我有一个应用程序想作为 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/

相关文章:

c# - 哪些文件必须在 SVN 存储库中才能让其他人能够编译?

c# - ASP.NET C# MySql 选择和更新表单

c# - 无法选择我的表单上的控件

.net - 取消数据绑定(bind)控件验证事件不会抑制更新数据源的尝试

.net - 当 InstanceContextMode.Single WCF 服务公开多个端点时存在多少个实例?

c# - 来自控制台应用程序的 HTTPS?

c# - .NET 是否具有用于多个集合的内置 IEnumerable?

.net - .NET-查询完成之前开始从DataReader读取?

c# - 如何将带有键/值的数组转换为 JSON c#

asp.net - 我们可以在 .net framework 4.5 中创建 ASMX WebService 吗?