当前位置:   article > 正文

dot net core 使用 IPC 进程通信

dot net core 使用 IPC 进程通信

本文告诉大家如何使用dot net core 和其他进程进行通信

一般都是使用 WCF 或 remoting 做远程通信,但是 dot net core 不支持 WCF 所以暂时我就只能使用 管道通信。

原理

管道通信使用的是 Pipe 需要启动一对才可以

在 NamedPipeServerStream 启动之后可以接受其他 NamedPipeClientStream 连接。

因为现在已经使用了 await 了,所以建议全部都可以写异步。

创建的 NamedPipeServerStream 需要告诉管道的命名,和通信方式,通信可以分为单向和双向,大家使用枚举去看一下就可以知道。

            var pipe = new NamedPipeServerStream("lindexi", PipeDirection.InOut);

上面代码就创建了一个管道,之后需要等待有连接才可以发送数据。

            await pipe.WaitForConnectionAsync();

等待了连接之后,就可以发送数据

  1. string str = "发送消息";
  2. var spxnfSrxldhhv = Encoding.UTF8.GetBytes(str);
  3. pipe.Write(spxnfSrxldhhv, 0, spxnfSrxldhhv.Length);

注意,需要指定发送数据的长度和数据,通过这个方法发送是不建议的。

如果需要使用一个比较高级的方法传输,请看文章最后

这时另一个程序就需要下面代码连接

  1. var pipe = new NamedPipeClientStream(".", "lindexi", PipeDirection.InOut, PipeOptions.None);
  2. pipe.Connect()

上面代码使用的 NamedPipeClientStream 需要指定管道的命名才可以找到。

连接之后可以通过这个方式读取数据

  1. var spxnfSrxldhhv = new byte[65535];
  2. var n = pipe.Read(spxnfSrxldhhv, 0, spxnfSrxldhhv.Length);
  3. var str = Encoding.UTF8.GetString(spxnfSrxldhhv, 0, n);

对于读写数据很多时候就使用 pipe 的读写,写入字节,读出字节。

因为一次读取可能会卡很多时间,所以建议使用异步读。

如果觉得每次发送都需要转 byte 然后进行写,代码很多,可以使用下面的代码

  1. var stream = new StreamWriter(pipe);
  2. stream.Write("发送消息");
  3. stream.Flush();

注意不要使用下面的代码

  1. using (var stream = new StreamWriter(pipe))
  2. {
  3. stream.Write("发送消息");
  4. }

原因是 using 会关闭 pipe 所以使用之后就不能在写。

例子

首先创建两个程序,一个是 WPF 程序 DgvlzKixtdin ,另一个是 dot net core 控制台 HclkvyDanuiag 。接着需要从 DgvlzKixtdin 发送数据,从 HclkvyDanuiag 接收数据。

在 WPF 程序添加一个 TextBlock 和 Button ,点击 Button 就发送消息到 dot net core 程序。收到消息就在 TextBlock 显示。

在 Button 点击的代码写下面代码

  1. private async void HixKkikjgp_OnClick(object sender, RoutedEventArgs e)
  2. {
  3. var pipe = new NamedPipeServerStream("lindexi", PipeDirection.InOut);
  4. await pipe.WaitForConnectionAsync();
  5. string str = "发送消息";
  6. var spxnfSrxldhhv = Encoding.UTF8.GetBytes(str);
  7. pipe.Write(spxnfSrxldhhv, 0, spxnfSrxldhhv.Length);
  8. spxnfSrxldhhv = new byte[100];
  9. var n = pipe.Read(spxnfSrxldhhv, 0, 100);
  10. str = Encoding.UTF8.GetString(spxnfSrxldhhv, 0, n);
  11. TjdsguhmKzj.Text = str;
  12. }

然后在 dot net core 程序写下面代码

  1. static void Main(string[] args)
  2. {
  3. Console.WriteLine("Hello World!");
  4. var pipe = new NamedPipeClientStream(".", "lindexi", PipeDirection.InOut, PipeOptions.None);
  5. pipe.Connect();
  6. var spxnfSrxldhhv = new byte[65535];
  7. var n = pipe.Read(spxnfSrxldhhv, 0, spxnfSrxldhhv.Length);
  8. var str = Encoding.UTF8.GetString(spxnfSrxldhhv, 0, n);
  9. Console.WriteLine(str);
  10. str = "收到";
  11. spxnfSrxldhhv = Encoding.UTF8.GetBytes(str);
  12. pipe.Write(spxnfSrxldhhv, 0, spxnfSrxldhhv.Length);
  13. Console.Read();
  14. }

先启动 WPF 程序,然后启动控制台

lindexi%2F2018411837142177.jpg

这时点击按钮之后就打开控制台可以看到控制台可以收到消息

lindexi%2F2018411838156388.jpg

然后 WPF 也收到控制台发过来的消息

lindexi%2F2018411838491360.jpg

序列化

虽然使用StreamWriter可以减少写入读取的代码,但是实际上这样只能用来传字符串,需要把类传输还是比较难,所以我找到了 Protobuf ,使用这个库可以简单使用。

首先打开 Nuget 安装 Protobuf 第一个

我这里使用 protobuf-net

然后创建一个类用来传输

  1. [ProtoContract]
  2. public class TyfxxTlkbjn
  3. {
  4. public string DczSwdsun { get; set; }
  5. }

使用下面代码可以进行写入

Serializer.Serialize(pipe, 实例);

所以修改一下上面的按钮按下

  1. {
  2. var pipe = new NamedPipeServerStream("lindexi", PipeDirection.InOut);
  3. await pipe.WaitForConnectionAsync();
  4. var tyfxxTlkbjn = new TyfxxTlkbjn()
  5. {
  6. DczSwdsun = "发送消息"
  7. };
  8. Serializer.Serialize(pipe, tyfxxTlkbjn);
  9. pipe.Disconnect();

修改 dot net core的代码

  1. static void Main(string[] args)
  2. {
  3. var pipe = new NamedPipeClientStream(".", "lindexi", PipeDirection.InOut, PipeOptions.None);
  4. pipe.Connect();
  5. var tyfxxTlkbjn = Serializer.Deserialize<TyfxxTlkbjn>(pipe);
  6. Console.WriteLine(tyfxxTlkbjn.DczSwdsun);
  7. Console.Read();
  8. }

如果使用 wcf 请看 .NET Core调用WCF的最佳实践

转载于:https://www.cnblogs.com/lindexi/p/dotnet-core-ipc.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/847708
推荐阅读
相关标签
  

闽ICP备14008679号