C# 传递 Windows 服务参数以供其操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/458674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 04:12:22  来源:igfitidea点击:

Passing a Windows Service Parameters for it to act on

c#.netparametersservice

提问by Malfist

I want to turn a program I have into a service so I can use it without logging it. Basically what it does is it backs up specified folders to a specified location using SSH. However the problem I'm running into is I don't know how to tell it these items. I only know how to start, stop, and run a custom command with only an integer with a parameter.

我想将我拥有的程序变成一项服务,这样我就可以在不记录的情况下使用它。基本上它的作用是使用 SSH 将指定的文件夹备份到指定的位置。然而,我遇到的问题是我不知道如何告诉它这些项目。我只知道如何启动、停止和运行只有一个带参数的整数的自定义命令。

How can I do this?

我怎样才能做到这一点?

Windows Service, not a Web Service

Windows 服务,而不是 Web 服务

edit: The folders it backs up will not remain consistent and will be updated at every runtime

编辑:它备份的文件夹不会保持一致,并且会在每次运行时更新

采纳答案by Otávio Décio

Any service is capable of receiving command line arguments at start-up.

任何服务都能够在启动时接收命令行参数。

回答by hmcclungiii

Would it be possible to use a configuration file to specify these items?

是否可以使用配置文件来指定这些项目?

回答by ChrisW

Store the service's startup parameters in the registry: and then, when the registry starts, it should read its startup parameters from the registry.

将服务的启动参数存储在注册表中:然后,当注册表启动时,它应该从注册表中读取它的启动参数。

回答by Dave Swersky

Windows services have executables like any other. I believe you can write it to accept command-line parameters and specify those parameters in the Windows Service configuration. You can also have it read a config file. If you're using .NET, there are config file classes in the framework.

Windows 服务与其他任何服务一样具有可执行文件。我相信您可以编写它来接受命令行参数并在 Windows 服务配置中指定这些参数。您也可以让它读取配置文件。如果您使用 .NET,则框架中有配置文件类。

回答by Kim Major

You can instantiate your service and pass command line arguments using the ServiceController class.

您可以使用 ServiceController 类实例化您的服务并传递命令行参数。

using (ServiceController serviceController = new ServiceController(serviceName))
{
   string[] args = new string[1];
   args[0] = "arg1";
   serviceController.Start(args);
}

"arg1" will then be available as regular command line arguments in main() when Windows starts up the service.

当 Windows 启动服务时,“arg1”将作为 main() 中的常规命令行参数可用。

回答by Sebastian

Why not just Host a WCF Service in the Windows Service to obatain such "admin" functions? (Remoting is also possible)

为什么不直接在 Windows 服务中托管一个 WCF 服务来获得这样的“管理”功能呢?(远程也可以)

回答by d7samurai

I see that you (or someone) voted Sebastian Sedlak's answer down, because he mentioned hosting a WCF Service in the Windows Service. Your reply was

我看到您(或某人)对 Sebastian Sedlak 的回答投了反对票,因为他提到在 Windows 服务中托管 WCF 服务。你的回复是

It's in nice bold lettering in the question. Not a Web Service, therefor WCF is out of the question

It's in nice bold lettering in the question. Not a Web Service, therefor WCF is out of the question

I think you misunderstood what he meant. He wasn't talking about a Web Service. He was talking about hosting a WCF Servicewithinyour Windows Service.

我想你误解了他的意思。他不是在谈论Web 服务。他正在谈论您的Windows 服务中托管WCF服务

It's far from the same thing. You can host a WCF Service withinany Windows (Forms/Console/Service) application. The point of doing so, is that the applicationis then reachable for communciation via its internal WCF Service, in the same fashionas you can communicate with a Web Service (you can alsohost WCF Services in IIS, btw, which would thenmake them "Web Services", in the sense you seem to be referring to).

它远非同一件事。您可以任何 Windows(窗体/控制台/服务)应用程序中托管 WCF 服务。这样做的问题是,该应用程序是则通过其内部WCF服务communciation到达,在相同的方式,你可以与Web Service(您可以沟通举办WCF服务在IIS,顺便说一句,这使他们“网络服务”,在您似乎指的意义上)。

In a Windows Service, this means you can send any command to it and also get any information you want from it - while it's running.

在 Windows 服务中,这意味着您可以向它发送任何命令并从中获取任何您想要的信息 - 在它运行时。

In fact, I am working on a project right now, which is a Windows Service that I need to be able to contact and pass commands to - and get information from - at runtime. For example, I want to be able to tell it where to store certain things, what to log, to have it reset/restart - and poll it for status messages. I do this by hosting a WCF Service inside the Windows Service. That WCF Service exposes a set of methods, that in my case includes receiving commands and returning status information. So when the Windows Serviceis running, I can contact it (even remotely), via its built-in WCF Serviceand tell it what to do.

事实上,我现在正在做一个项目,这是一个 Windows 服务,我需要能够在运行时联系和传递命令 - 并从中获取信息。例如,我希望能够告诉它在哪里存储某些东西,记录什么,让它重置/重新启动 - 并轮询它以获取状态消息。我通过在 Windows 服务中托管 WCF 服务来做到这一点。WCF 服务公开了一组方法,在我的例子中包括接收命令和返回状态信息。因此,当Windows 服务运行时,我可以通过其内置的WCF 服务联系它(甚至远程)并告诉它要做什么。

This an extremely easy thing to implement, and in the case of Windows Services, can provide you with a much richer interface to the Service than through the basic standard commands.


However, you specified that you wanted your Windows Service to receive its folder settoings each time it starts up, which makes such a passive setup less than ideal (as it would be unable to do anything until you passed it the right folders).

这是一个非常容易实现的事情,并且在 Windows 服务的情况下,可以为您提供比通过基本标准命令更丰富的服务接口。


但是,您指定您希望 Windows 服务在每次启动时接收其文件夹设置,这使得这种被动设置不太理想(因为在您将正确的文件夹传递给它之前它将无法执行任何操作)。

One way to deal with this (using a hosted WCF Service), would be to have the Windows Service running all the time(i.e. automatic startup). Its default state would be idle. Then you could issue it a "start processing"-command, feeding it the correct folders to work on (through a call to the corresponding WCF Service method). Similarly, the WCF Service would expose methods giving you the status of the application (current folder, progress, busy/idle etc). Once the processing is done, it would go back into the idle state, waiting for the next set of folders to be supplied to it.

处理此问题的一种方法(使用托管 WCF 服务)是让 Windows 服务始终运行(即自动启动)。它的默认状态是空闲。然后,您可以向它发出“开始处理”命令,为它提供要处理的正确文件夹(通过调用相应的 WCF 服务方法)。同样,WCF 服务会公开一些方法,为您提供应用程序的状态(当前文件夹、进度、忙碌/空闲等)。处理完成后,它将返回空闲状态,等待提供给它的下一组文件夹。

Doing it this way would make it very easy to control remotely - you could even make an online administration panel for it, accessible from anywhere.

这样做可以让远程控制变得非常容易——你甚至可以为它制作一个在线管理面板,可以从任何地方访问。

回答by Karl

The issue, is that, while passing in parameters is not difficult, when the machine restarts and windows tries to restart the service, those parameters are not there. they only exist when someone starts the service from the command line.

问题是,虽然传入参数并不困难,但当机器重新启动并且 Windows 尝试重新启动服务时,这些参数不存在。它们仅在有人从命令行启动服务时才存在。

for example. I have a windows service which hosts a WCF service. I want the users to be able to specify a non-default port number for the WCF service to listen on. They do this by starting the windows service like so... MyService -port:xxxxx

例如。我有一个承载 WCF 服务的 Windows 服务。我希望用户能够为 WCF 服务指定一个非默认端口号来监听。他们通过像这样启动 Windows 服务来做到这一点...... MyService -port:xxxxx

Which works fine, until the server is rebooted, then windows restarts MyService (but without parameters) and the wcf service defaults to original port #

哪个工作正常,直到服务器重新启动,然后 windows 重新启动 MyService(但没有参数)并且 wcf 服务默认为原始端口 #

回答by Carsten

Concerning the app.config file - I'm rather sure that your service will read and use those files as I write all my windows-services this way ;) So just put everything you need in the app.config under "application" (not user) and put allway edit the "yourname.exe.config" in the folder where you "InstallUtil" the service from.

关于 app.config 文件 - 我很确定你的服务会读取和使用这些文件,因为我以这种方式编写我所有的 Windows 服务;) 所以只需将你需要的所有内容放在 app.config 下的“应用程序”下(不是用户)并在您“InstallUtil”服务所在的文件夹中编辑“yourname.exe.config”。

回答by David Wazy

RE: config file.

回复:配置文件。

Of course a config file can be used. And the file can be changed while the service is running.

当然可以使用配置文件。并且可以在服务运行时更改文件。

This would be a nice solution if the config file changes in fact. All my services use an XML config file, wrapped in a class for easy reuse.

如果配置文件实际上发生了变化,这将是一个很好的解决方案。我所有的服务都使用一个 XML 配置文件,封装在一个类中以便于重用。

The wrapper has an option to monitor the XML file using fileMonitor for changes, optionally refreshing the content of the config file automatically, and finally raises an event to the service class. The service then has the option of "resetting" itself as needed to incorporate the new values in the XML configuration file.

包装器可以选择使用 fileMonitor 监视 XML 文件的更改,可选择自动刷新配置文件的内容,最后向服务类引发事件。然后,该服务可以根据需要“重置”自身以将新值合并到 XML 配置文件中。

Placing configuration into the registry has a few issues:

将配置放入注册表有几个问题:

  • Security (ie: installer being granted access), depending on what tree is used
  • The service will not be aware of changes
  • Portability - although minor as the install should setup registry settings
  • 安全性(即:安装程序被授予访问权限),取决于使用的树
  • 该服务不会意识到变化
  • 可移植性 - 虽然安装应该设置注册表设置,但次要

Where an XML file is easy to copy, edit, share, view and understand. Throw in some good COMMENT blocks and a detailed XSD file, and it becomes a source of good documentation too.

XML 文件易于复制、编辑、共享、查看和理解。加入一些好的 COMMENT 块和详细的 XSD 文件,它也会成为好的文档的来源。

Look into XPath for easy navigation and extraction of values within the XML file.

查看 XPath 以轻松导航和提取 XML 文件中的值。

$0.02 ... david ...

0.02 美元……大卫……