2012-08-31 51 views
0

我需要能够在单台机器上多次安装相同的服务。 那部分我有工作!但我也需要ServiceName的不同。那部分不是。ServiceName没有正确更改

下面是我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输出到个人日志文件。但它说,服务名称是始终AuditStreamService

我希望有说AuditStream-NW在这种情况下...任何人都可以看到我有错吗?

EXTRA: 我想这些名称不同的原因是因为每个服务还创建了一个MemoryMappedFile,和我原本它设置,使非持续性MMF的名字总是"AuditStream-" + HubName(这是配置中的确定文件),但现在外部程序将通过读取mmf来监视服务正在执行的操作,但除了读取服务配置文件外,外部应用程序不知道mmf的名称。我的目标是使所有的名字相同,ServiceName = MMF Name = ServiceDisplayName

+0

它实际上安装为(检查services.msc)是什么?我敢打赌它正确安装,但你的日志行从错误的地方获取ServiceName。 –

+0

内SERVICES.MSC好是列为AuditStream-NW像我想要的,但是我觉得这个名字的显示名称... – Zholen

回答

0

好吧,事实证明,我的安装过程很好,我只是不能在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; 
}