C# 如何从运行的 Windows 服务调用方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1979016/
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
How to call method from running windows service
提问by IrfanRaza
I have created and started windows service Service1 (with exe as MyService.exe) using c# 2005. . I have included a method GetMyRandomNumber() that returns a random double value.
我已经使用 c# 2005 创建并启动了 Windows 服务 Service1(使用 exe 作为 MyService.exe)。. 我已经包含了一个返回随机双精度值的方法 GetMyRandomNumber()。
The problem here is how could use this running service and how could i call the method.
这里的问题是如何使用这个正在运行的服务以及我如何调用该方法。
I have tried adding reference of MyService.exe and access the method as -
我尝试添加 MyService.exe 的引用并将该方法访问为 -
Service1 s = new Service1();
MessageBox.Show(s.GetMyRandomNumber().ToString());
But found that the method is not called from the running instance of the service i.e. even though i stop the service the statements are executed.
但是发现该方法不是从服务的运行实例调用的,即即使我停止服务,语句也会被执行。
Could someone explain me how can I call the method from running instance of the service.
有人可以解释我如何从运行的服务实例调用该方法。
Thanks for sharing your valuable time.
感谢您分享您的宝贵时间。
采纳答案by Adriaan Stander
You should have a look at Remoting
你应该看看远程处理
- Remoting in C# (Link no Longer available)
- How To Host .NET Remoting Objects in Windows Service Application
- How to Use .Net Remoting Using (Link no Longer available)
- A Simple Introduction to .NET Remoting
- C# 中的远程处理(链接不再可用)
- 如何在 Windows 服务应用程序中托管 .NET 远程处理对象
- 如何使用 .Net Remoting Using(链接不再可用)
- .NET 远程处理的简单介绍
回答by Andy West
You could use Windows Communication Foundationand IPC (inter-process communication) to communicate with your service and execute your method.
您可以使用Windows Communication Foundation和 IPC(进程间通信)与您的服务进行通信并执行您的方法。
回答by Alan
In your code, you aren't actually calling the service, instead you are referencing the executable and invoking a method from that assembly (at run time the .NET Framework will use a local assembly to execute the code, not your running service).
在您的代码中,您实际上并不是在调用服务,而是在引用可执行文件并从该程序集中调用方法(在运行时,.NET Framework 将使用本地程序集来执行代码,而不是您正在运行的服务)。
To do what you want, you have a number of options.
要做您想做的事,您有多种选择。
In .NET 2.0, you would make use of .NET Remoting. You create a remoting interface, which other assemblies can use to invoke methods across executables.
在 .NET 2.0 中,您将使用 .NET Remoting。您创建一个远程处理接口,其他程序集可以使用该接口跨可执行文件调用方法。
In .NET 3.0, remoting was replaced by WCF. Your service would become a WCF service, which would expose the GetRandomNumber() as part of its data contract. Applications can consume the contract and connect to your service to call the method.
在 .NET 3.0 中,远程处理被 WCF 取代。您的服务将成为 WCF 服务,它将公开 GetRandomNumber() 作为其数据契约的一部分。应用程序可以使用合约并连接到您的服务以调用该方法。
There are a number of good tutorials on the web for both .NET Remoting or its replacement, Windows Communication Foundation.
网络上有许多关于 .NET Remoting 或其替代品 Windows Communication Foundation 的优秀教程。
回答by Mark Seemann
Communicating with a running service is no different from invoking methods on any other running process. That means that you will need to dig out your standard tools for process-to-process communication.
与正在运行的服务进行通信与在任何其他正在运行的进程上调用方法没有什么不同。这意味着您将需要挖掘用于流程到流程通信的标准工具。
Windows Communication Foundation(WCF) would be my default choice. You can host a WCF service in your Windows Service and expose it through a Named Pipe endpoint for efficient communication.
Windows Communication Foundation(WCF) 将是我的默认选择。您可以在 Windows 服务中托管 WCF 服务,并通过命名管道端点公开它以实现高效通信。