C# 4.0 ServiceName未正确更改

C# 4.0 ServiceName未正确更改,c#-4.0,windows-services,C# 4.0,Windows Services,我需要能够在一台机器上多次安装相同的服务。 那部分我有工作!但我也需要服务名的不同。那部分不是 以下是my Installer.cs中的代码: [RunInstaller(true)] public partial class ProjectInstaller : System.Configuration.Install.Installer { public ProjectInstaller() { InitializeComponent(); }

我需要能够在一台机器上多次安装相同的服务。 那部分我有工作!但我也需要服务名的不同。那部分不是

以下是my Installer.cs中的代码:

[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
    public ProjectInstaller()
    {
        InitializeComponent();
    }

    public override void Install(IDictionary stateSaver)
    {
        RetrieveServiceName();
        base.Install(stateSaver);
    }

    public override void Uninstall(IDictionary savedState)
    {
        RetrieveServiceName();
        base.Uninstall(savedState);
    }

    private void RetrieveServiceName()
    {
        var serviceName = Context.Parameters["servicename"];
        if(!string.IsNullOrEmpty(serviceName))
        {
            auditStreamServiceInstaller.ServiceName = serviceName;
            auditStreamServiceInstaller.DisplayName = serviceName;
        }
    }
}
我使用下面的cmd来安装服务

C:\Windows\Microsoft.Net\Framework\v4.0.30319> installutil /servicename="AuditStream-NW" d:AuditStreamService.exe
现在,如果我查看installlog:

Affected parameters are:
   logtoconsole = 
   logfile = C:\AuditStreams\NW\AuditStreamService.InstallLog
   assemblypath = C:\AuditStreams\NW\AuditStreamService.exe
   servicename = AuditStream-NW
这看起来是正确的,但在服务的OnStart中,我有一行代码将ServiceName输出到个人日志文件。但是它说ServiceName始终是AuditStreamService

我希望在这种情况下能有这样的说法……有人能看出我做错了什么吗

额外:
我希望这些名称不同的原因是,每个服务还创建了一个MemoryMappedFile,最初我将其设置为非持久性mmf的名称始终是
“AuditStream-”+HubName
(在配置文件中确定),但是外部程序现在将通过读取mmf来监视服务正在做什么,但是除了读取服务配置文件外,外部应用程序不知道mmf的名称。我的目标是使所有名称都相同,
ServiceName=MMF Name=servicesplayName

好的,因此我的安装过程很好,我只是不能在OnStart()中使用this.ServiceName变量,因为它将始终返回通用默认名称,而不是在安装过程中选择的名称。以下代码是我用来获取真实姓名的代码:

int myPid = Process.GetCurrentProcess().Id;
var services = ServiceController.GetServices();
foreach (var service in services)
{
    ManagementObject wmiService = new ManagementObject("Win32_Service.Name='" + service.ServiceName + "'");
    wmiService.Get();
    if (Convert.ToInt32(wmiService["ProcessId"]) == myPid)
        myServiceName = service.ServiceName;
}

它实际安装为什么(检查services.msc)?我敢打赌它安装正确,但您的日志行从错误的位置获取ServiceName。在Services.msc中,它是否如我所希望的那样列为AuditStream NW,但我认为此名称是显示名称。。。