windows 不同基于 C# 的服务之间的通信

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5486917/
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-09-15 16:31:27  来源:igfitidea点击:

Communication between different C# based services

c#windowswindows-servicesinterprocess

提问by elisa

Is there a way to communicate between two different services? I have a service that already runs. Is there a way to create a second service that can attach to the first service and send and receive dates to it?

有没有办法在两个不同的服务之间进行通信?我有一个已经运行的服务。有没有办法创建可以附加到第一个服务并向其发送和接收日期的第二个服务?

I would also like to access the Windows service from a console application and attach to it. Is it possible?

我还想从控制台应用程序访问 Windows 服务并附加到它。是否可以?

采纳答案by James Kyburz

To begin with I would play around with tcpclient and tcpserver

首先我会玩 tcpclient 和 tcpserver

http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspxhttp://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.aspx

http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.aspx

Even if the data you need to send is more complex than a date it can easily be serialized/deserialized.

即使您需要发送的数据比日期更复杂,也可以轻松地对其进行序列化/反序列化。

For sending and receiving dates this seams the simplest option.

对于发送和接收日期,这是最简单的选项。

Also socks work if the services run on different machines whereas shared memory and namedpipes don't.

如果服务在不同的机器上运行,而共享内存和命名管道则不能,socks 也可以工作。

example code

示例代码

// Create a thread running this code in your onstarted method of the service

using System.IO;
using System.Net;
using System.Net.Sockets;

var server = new TcpListener(IPAddress.Parse("127.0.0.1"), 8889);
server.Start();

while(true) {
  var client = server.AcceptTcpClient(); 

  using(var sr = new StreamReader(client.GetStream())) {
    var date = DateTime.Parse(sr.ReadToEnd());
    Console.WriteLine(date);
  } 
}

// In the console

using System.IO;
using System.Net;
using System.Net.Sockets;

var client = new TcpClient("localhost",8889); 
using(var sw = new StreamWriter(client.GetStream())) {
  sw.Write(System.DateTime.Now);
}

回答by HABJAN

You can try to implement this by using:

您可以尝试使用以下方法来实现:

Example of using WCF: Many to One Local IPC using WCF and NetNamedPipeBindin.

使用 WCF 的示例:使用 WCF 和 NetNamedPipeBindin 的多对一本地 IPC

Other example: A C# Framework for Interprocess Synchronization and Communication.

其他示例:用于进程间同步和通信的 AC# 框架

Everything depends on what version of .NET Framework you use. If you use .NET 3.0 and above then you can take a look into WCF. If not then you are on your own and you can google on keywords P/Invoke(CreateFileMapping, MapViewOfFile, CreatePipe...).

一切都取决于您使用的 .NET Framework 版本。如果您使用 .NET 3.0 及更高版本,那么您可以查看WCF。如果没有,那么您就靠自己了,您可以在关键字P/Invoke(CreateFileMapping、MapViewOfFile、CreatePipe...)上搜索。