一个Windows服务中的多个服务进程(System.ServiceProcess.ServiceBase) [英] Multiple service processes (System.ServiceProcess.ServiceBase) in one Windows Service
问题描述
我有两个服务流程(源自 System.ServiceProcess.ServiceBase
)
MyService1
和 MyService2
。
I've got two service processes (derived from System.ServiceProcess.ServiceBase
)
MyService1
and MyService2
.
我正在尝试在 Main()中运行它们
Windows服务的 Programm.cs
。
I'm trying to run them both in the Main()
of a Windows Service's Programm.cs
.
static void Main()
{
ServiceBase[] servicesToRun = { new MyService1(), new MyService2() };
ServiceBase.Run(servicesToRun);
}
在 OnStart
我将 MyService1
和 MyService2
的方法写入日志文件,以便告诉它们正在运行。
In the OnStart
methods of both MyService1
and MyService2
I write to a log file so I can tell they are running.
系统正常运行,我可以安装服务。
The system builds fine and I can install the service.
但只有 MyService1
运行。 MyService2
不执行任何操作(即没有启动日志条目)。当我更改数组中的顺序时:
But only MyService1
runs. MyService2
doesn't do a thing (i.e. no start-up log entry). When I change the order in the array:
ServiceBase[] servicesToRun = { new MyService2(), new MyService1() }
仅运行 MyService2
。
要尝试深入了解这一点,我使用了一个小工具AndersonImes.ServiceProcess.ServicesLoader( https://windowsservicehelper.codeplex.com/ )来解决无法在Visual Studio中直接调试Windows服务的限制。使用此工具,我可以使两个服务 MyService1
和 MyService2
相互启动并运行。但是我仍然不知道为什么Windows仅运行 ServiceBase [] servicesToRun
数组中的第一项。
To try to get to the bottom of this, I'm using a little tool AndersonImes.ServiceProcess.ServicesLoader (https://windowsservicehelper.codeplex.com/) to get around the limitation that you cannot directly debug a windows service in Visual Studio. With this tool I can get both services MyService1
and MyService2
to start and run next to each other. But I still don't know why Windows is running only the first item in the ServiceBase[] servicesToRun
array.
有任何想法吗?
推荐答案
我终于在这里找到了答案: http://www.bryancook.net/2008/04/running-multiple-net-services-within.html 一个>。隐藏的资源,谢谢布莱恩!希望这可以帮助下一个开发人员节省时间...
I finally found the answer here: http://www.bryancook.net/2008/04/running-multiple-net-services-within.html. A well hidden resource, thanks 'bryan'! Hopefully this helps the next developer to save time...
关于ServicesDependedOn的解释与我在项目中看到的并不完全匹配。这不是要启动它们,而是要确保它们已启动。查看 https:/ /msdn.microsoft.com/zh-CN/library/system.serviceprocess.servicecontroller.servicesdependedon%28v=vs.110%29.aspx 。我不需要这个,因为它们彼此不依赖。
The explanation there around ServicesDependedOn isn't quite matching what I see in my project though. It's not about starting them but making sure they are started. Check out https://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.servicesdependedon%28v=vs.110%29.aspx as well. I don't need this because they do not depend on each other.
这篇关于一个Windows服务中的多个服务进程(System.ServiceProcess.ServiceBase)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!