创建和使用 C# Windows 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6859848/
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
Creating and Consuming a C# Windows Service
提问by Jeffrey Kevin Pry
I have a Windows 2008 R2 Server running IIS 7.5. Currently, I use WCF to expose an interface to the outside where the code then calls routines from my DLLs.
我有一台运行 IIS 7.5 的 Windows 2008 R2 服务器。目前,我使用 WCF 向外部公开一个接口,然后代码从我的 DLL 调用例程。
I would like to move away from this direct access and create some sort of daemon in C# that I can run in the background. I will use the daemon to monitor threads, accept requests, and balance performance. I plan on allowing the daemon full access to the DLLs of my main application and then will have WCF services pass on commands to the daemon as they are received.
我想摆脱这种直接访问,并在 C# 中创建某种可以在后台运行的守护进程。我将使用守护进程来监视线程、接受请求和平衡性能。我计划允许守护程序完全访问我的主应用程序的 DLL,然后让 WCF 服务在收到命令时将命令传递给守护程序。
I have looked on the Internet and found a few examples about creating a Windows Service, building installers, and registering the service; however, I cannot seem to find any documentation on how to interact with running services via a different application.
我在网上找了几个关于创建 Windows 服务、构建安装程序和注册服务的例子;但是,我似乎找不到任何关于如何通过不同的应用程序与正在运行的服务进行交互的文档。
Here's more or less an example of what I am looking to do:
这里或多或少是我想要做的一个例子:
Let's say I've built and installed the sample service depicted here: http://blogs.msdn.com/b/bclteam/archive/2005/03/15/396428.aspx
假设我已经构建并安装了此处描述的示例服务:http: //blogs.msdn.com/b/bclteam/archive/2005/03/15/396428.aspx
Now, a customer tells me I need to extend it to allow jobs to run on demand. I have built the necessary functions to allow me to do so, but now I am faced with the issue of determining how to talk to the currently running service to tell it to start processing. How do I do this? Do you have any example links that describe this IPC ?
现在,一位客户告诉我,我需要扩展它以允许作业按需运行。我已经构建了必要的函数来允许我这样做,但现在我面临着确定如何与当前运行的服务对话以告诉它开始处理的问题。我该怎么做呢?您是否有任何描述此 IPC 的示例链接?
Thanks!
谢谢!
回答by Davide Piras
You can use WCF to communicate with your Windows service.
您可以使用 WCF 与您的 Windows 服务进行通信。
In fact let's say you have a class library with some APIs you would like to make available like a daemom on the network, no user login required on that machine...
事实上,假设您有一个包含一些 API 的类库,您希望像网络上的守护进程一样可用,该机器上不需要用户登录......
you can have a WCF end-point which exposes the APIs to the callers and either host this service in IIS or in a Windows service, the only difference is the way you host the WCF (IIS does it for you with the usage of .svc file, Windows Service requires a bit of code to start the WCF hosting object); after this, nearly everything stays the same.
您可以拥有一个 WCF 端点,它将 API 公开给调用者,并在 IIS 或 Windows 服务中托管此服务,唯一的区别是您托管 WCF 的方式(IIS 使用 .svc 为您完成它)文件,Windows 服务需要一些代码来启动 WCF 托管对象);在此之后,几乎所有内容都保持不变。
回答by Peter
You can send custom commands to a Windows Service, but these are only integer values:
您可以向 Windows 服务发送自定义命令,但这些只是整数值:
protected override void OnCustomCommand(int command)
A better solution is to communicate with the Windows Service over "named pipes". Best way of implementing this is IMHO to create a WCF service with NetNamedPipe binding and host it in the Windows Service itself.
更好的解决方案是通过“命名管道”与 Windows 服务通信。实现这一点的最佳方法是恕我直言,使用 NetNamedPipe 绑定创建 WCF 服务并将其托管在 Windows 服务本身中。
回答by Tomas Jansson
Have you looked at this: http://msdn.microsoft.com/en-us/library/ms733069.aspx
你看过这个:http: //msdn.microsoft.com/en-us/library/ms733069.aspx
Also, if you would like an easy way to debug your service in visual studio I've written a blog post about it: http://blog.tomasjansson.com/2010/11/debugging-your-windows-service-in-visual-studio/
另外,如果您想要一种在 Visual Studio 中调试服务的简单方法,我已经写了一篇关于它的博客文章:http: //blog.tomasjansson.com/2010/11/debugging-your-windows-service-in-视觉工作室/
回答by Brian
You have to use a ServiceController. Once you have a ServiceController instance you can invoke ExecuteCommand(int). ExecuteCommand will invoke OnCustomCommand in your service instance as long as your command isn't the Start/Stop/Pause. Just override OnCustomCommand and you should be set...
您必须使用 ServiceController。一旦有了 ServiceController 实例,就可以调用 ExecuteCommand(int)。只要您的命令不是开始/停止/暂停,ExecuteCommand 就会在您的服务实例中调用 OnCustomCommand。只需覆盖 OnCustomCommand ,您就应该设置...
Reference for ServiceController:
ServiceController 的参考:
http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.aspx
http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.aspx
回答by Scott Chamberlain
There is nothing magical about a service, it is just a program that starts on boot that is run by the system instead of the user. You need to program in to your service some form of IPC (be it something as simple as configuration files or something more advanced like loading all dlls from a directory and calling a known function as a entry point (This would be the plugin model).
服务并没有什么神奇之处,它只是一个在启动时由系统而不是用户运行的程序。您需要将某种形式的 IPC 编程到您的服务中(可以是像配置文件一样简单的东西,也可以是更高级的东西,例如从目录加载所有 dll 并调用已知函数作为入口点(这将是插件模型)。
You will need to write your original service to listen in some form to provide the funcationality you want.
您将需要编写您的原始服务以某种形式收听以提供您想要的功能。
回答by Yahia
IPC can be done with different methods:
IPC 可以用不同的方法完成:
TCP/IP
Your service exposes a WCF/HTTP whatever interface (for example usingServiceHost
/HttpListener
or evenServerSocket
) - these can be local and/or remote accessibleNamedPipe
You can use a named Pipe for communication - these can be local and/or remote accessible
see http://msdn.microsoft.com/en-us/library/system.io.pipes.namedpipeserverstream.aspxMemoryMappedFile
Is extremely fast, works only local and needs some sort of synchronization (like aMutex
or similar)
see http://weblogs.asp.net/gunnarpeipman/archive/2009/06/21/net-framework-4-0-using-memory-mapped-files.aspxand http://msdn.microsoft.com/en-us/library/dd997372.aspx
TCP/IP
您的服务公开 WCF/HTTP 任何接口(例如使用ServiceHost
/HttpListener
甚至ServerSocket
) - 这些可以是本地和/或远程访问NamedPipe
您可以使用命名管道进行通信 - 这些管道可以是本地和/或远程访问的,
请参见http://msdn.microsoft.com/en-us/library/system.io.pipes.namedpipeserverstream.aspxMemoryMappedFile
速度极快,仅在本地工作,需要某种同步(如 aMutex
或类似),
请参阅http://weblogs.asp.net/gunnarpeipman/archive/2009/06/21/net-framework-4-0-using-内存映射文件.aspx和http://msdn.microsoft.com/en-us/library/dd997372.aspx
回答by Mike
So you need a windows service that performs a number of scheduled actions and also can listen for requests via WCF to perform on demand actions, did I get that right?
因此,您需要一个 Windows 服务来执行许多计划操作,并且还可以通过 WCF 侦听请求以执行按需操作,我做对了吗?
Here's an MSDN document explaining how to create a windows service that implements a WCF service: http://msdn.microsoft.com/en-us/library/ms733069.aspx
这是解释如何创建实现 WCF 服务的 Windows 服务的 MSDN 文档:http: //msdn.microsoft.com/en-us/library/ms733069.aspx