ServiceName更改不正确 [英] ServiceName is not changing properly

查看:122
本文介绍了ServiceName更改不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够在一台计算机上多次安装相同的服务。
我正在工作的那部分!但是我还需要ServiceName有所不同。

I have a need to be able to install the same service multiple times on a single machine. That part I have working! But I also need the ServiceName's to be different. That part is not.

下面是我的Installer.cs中的代码:

Below is the code within 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安装服务

and I use the following cmd to install the service

C:\Windows\Microsoft.Net\Framework\v4.0.30319> installutil /servicename="AuditStream-NW" d:AuditStreamService.exe

现在,如果我看一下installlog :

Now if I look at the installlog :

Affected parameters are:
   logtoconsole = 
   logfile = C:\AuditStreams\NW\AuditStreamService.InstallLog
   assemblypath = C:\AuditStreams\NW\AuditStreamService.exe
   servicename = AuditStream-NW

这看起来是正确的,但是在我的服务的OnStart内,有一行将ServiceName输出到个人日志文件。但是它说ServiceName始终是AuditStreamService

This looks correct, but within my OnStart of my service, I have a line that outputs the ServiceName to a personal log file. But it says that the ServiceName is always AuditStreamService

在这种情况下,我希望这样说AuditStream-NW ...有人能看到我错了吗?

I was hoping to have that say AuditStream-NW in this case...Can anyone see what I've got wrong?

附加:
我想要这些名称不同的原因是因为每个服务还创建了一个MemoryMappedFile,并且最初我已对其进行设置因此,该非持久性mmf的名称始终为 AuditStream- + HubName (在配置文件中确定),但是外部程序现在将监视服务是通过读取mmf来完成的,但是除了读取服务配置文件之外,外部应用程序不知道mmf的名称。我的目标是使所有名称相同, ServiceName = MMF名称= ServiceDisplayName

EXTRA: The reason I want these names different is because each service also creates a MemoryMappedFile, and originally I had it setup so the name of that non-persistant mmf was always "AuditStream-" + HubName(which is determined within the config file), but an outside program now will be monitoring what the service is doing by reading the mmf, but aside from reading the services config file the external application doesnt know the name of the mmf. My goal is to make all the names the same, ServiceName = MMF Name = ServiceDisplayName.

推荐答案

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

Ok so it turns out that my installation processes was fine, I just simply cannot use the this.ServiceName variable within the OnStart() as it will always return the generic default name and not the name that was chosen during installation. The following code is what I used to acquire my true name:

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;
}

这篇关于ServiceName更改不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆