ServiceName is not changing properly

£可爱£侵袭症+ 提交于 2019-12-13 07:22:28

问题


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.

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

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

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

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

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

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.


回答1:


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


来源:https://stackoverflow.com/questions/12207355/servicename-is-not-changing-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!