如何配置安装在Windows服务的名称(或容易在编译时)? [英] How do I configure the name of a Windows service upon installation (or easily at compile time)?

查看:214
本文介绍了如何配置安装在Windows服务的名称(或容易在编译时)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中创建一个Windows服务,在服务器上安装了它,它运行良好。

I've created a Windows service in C#, installed it on a server and it is running fine.

现在我想重新安装相同的服务,但是从不同的工作目录下运行,具有不同的配置文件等。因此,我想有的两个的(或以上)的情况下,同样的服务同时运行。 最初,这是不可能的,因为安装程序会抱怨有已经安装了给定名称的服务。

Now I want to install the same service again, but running from a different working directory, having a different config file etc. Thus, I would like to have two (or more) instances of the same service running simultaneously. Initially, this isn't possible since the installer will complain that there's already a service with the given name installed.

我可以改变我的code时, ServiceBase.ServiceName 属性设置为一个新的值,然后重新编译和重新运行InstallUtil.exe克服这一点。不过,我更preFER如果我能定在安装时的服务名称,即理想的我会做这样的事情

I can overcome this by changing my code, setting the ServiceBase.ServiceName property to a new value, then recompiling and running InstallUtil.exe again. However, I would much prefer if I could set the service name at install-time, i.e. ideally I would do something like

InstallUtil.exe / I   / SERVICENAME =为MyService实例2   MyService.exe

InstallUtil.exe /i /servicename="MyService Instance 2" MyService.exe

如果这是不可能实现的(我很怀疑),我希望能在我构建服务注入的服务名称。我想这可能是可能使用了某种版本的事件,用一个聪明的MSBuild或楠伎俩或类似的规定,但我没有得到一个线索。

If this isn't achievable (I very much doubt it), I would like to be able to inject the service name when I build the service. I thought it might be possible to use some sort of build event, use a clever msbuild or nant trick or something along those lines, but I haven't got a clue.

任何建议将是极大的AP preciated。

Any suggestions would be greatly appreciated.

感谢您的时间。

推荐答案

我试图用访问配置。

ConfigurationManager.OpenExeConfiguration(string exePath)

在安装程序,但无法得到它的工作。

in the installer, but couldn't get it to work.

相反,我决定使用 System.Environment.GetCommandLineArgs()在这样的安装程序:

Instead I decided to use System.Environment.GetCommandLineArgs() in the installer like this:

string[] commandlineArgs = Environment.GetCommandLineArgs();

string servicename;
string servicedisplayname;
ParseServiceNameSwitches(commandlineArgs, 
    out servicename, 
    out servicedisplayname);

serviceInstaller.ServiceName = servicename;
serviceInstaller.DisplayName = servicedisplayname;

现在,我可以用我的安装服务

Now I can install my services using

InstallUtil.exe / I   InstallableService.dll   / SERVICENAME =myserviceinstance_2   / servicedisplayname =我的服务   实例2

InstallUtil.exe /i InstallableService.dll /servicename="myserviceinstance_2" /servicedisplayname="My Service Instance 2"

我这里写了一个更详细的解释

I wrote up a more elaborate explanation here.

这篇关于如何配置安装在Windows服务的名称(或容易在编译时)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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