简体   繁体   English

在Windows服务上调用公共方法

[英]Calling a public method on windows service

I have a timer in a windows service (.NET C#). 我在Windows服务(.NET C#)中有一个计时器。 I need to be able to change the elapsed value from an external program. 我需要能够从外部程序更改已用值。 If the timer is currently running, I need to be able to shorten the time elapsed value from an external program. 如果计时器当前正在运行,我需要能够缩短外部程序的经过时间值。 I was thinking of having a public method in the service that would change the timer elapsed value and restart the timer, but can an external program call a public method on a windows service? 我想在服务中使用一个公共方法来改变计时器经过的值并重新启动计时器,但是外部程序可以在Windows服务上调用公共方法吗?

In short, it's not possible to directly call functions in another process. 简而言之,不可能在另一个进程中直接调用函数。 The process containing the function you want to access (in this case, your Windows service) will need to expose it through some sort of IPC (inter-process communication). 包含您要访问的功能(在本例中为Windows服务)的进程需要通过某种IPC(进程间通信)公开它。 What type of IPC you choose will probably depend on how complex the communication needs to be, and whether or not the "client" is a .NET application. 您选择哪种类型的IPC可能取决于通信需要多复杂,以及“客户端”是否是.NET应用程序。

If your needs are simple (eg just setting a timer value), or if your client does not use .NET, using named pipes (or TCP, if you need to access the service from another physical machine) is probably your best bet. 如果您的需求很简单(例如,只设置计时器值),或者您的客户端不使用.NET,则使用命名管道(或TCP,如果您需要从另一台物理机访问该服务)可能是您最好的选择。 Both named pipes and TCP give you a Stream that you can write messages to and read on the other end. 命名管道和TCP都为您提供了一个Stream,您可以将消息写入另一端并在另一端读取。

If you need to expose many different functions or send and receive complex data types, and if you are using .NET on both ends, .NET Remoting or WCF is probably best. 如果您需要公开许多不同的功能或发送和接收复杂的数据类型,并且如果您在两端都使用.NET,那么.NET Remoting或WCF可能是最好的。 .NET Remoting is simpler but has more constraints; .NET Remoting更简单但有更多限制; WCF is very flexible but has a steeper learning curve. WCF非常灵活,但学习曲线更陡峭。

Yes this is possible. 是的,这是可能的。

You might want to consider created a NetNamedPipe endpoint on your service and controlling the service through that interface. 您可能需要考虑在服务上创建NetNamedPipe端点并通过该接口控制服务。

NetNamedPipeBinding binding = new NetNamedPipeBinding();
MyService myService = new MyService(binding,
              new EndpointAddress("net.pipe://localhost/MyService"));
myService.ResetTimer(30);

You cannot call a method in a Windows service process directly, but you can have the Windows service expose this function as a WCF service, for instance. 您无法直接在Windows服务进程中调用方法,但是您可以让Windows服务将此功能公开为WCF服务。 The Windows service would be acting as a service host as well. Windows服务也将充当服务主机。 That's possible and not complicated. 这是可能的,而不是复杂的。

Older (non WCF) services can use .NET Remoting. 较旧(非WCF)服务可以使用.NET Remoting。 Have a look here for some info on how to get started. 看看这里有关如何入门的一些信息。 This is the pre-WCF way of communicating between applications across process boundaries. 这是跨进程边界在应用程序之间进行通信的WCF前方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM