WCF实现进程间管道通信Demo分享
更新时间:2020年6月25日 11:17 点击:1430
一、代码结构:
二、数据实体类:
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; namespace DataStruct { /// <summary> /// 测试数据实体类 /// </summary> [DataContract] public class TestData { [DataMember] public double X { get; set; } [DataMember] public double Y { get; set; } } }
三、服务端服务接口和实现:
接口:
using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Text; using System.Threading.Tasks; using DataStruct; namespace WCFServer { /// <summary> /// 服务接口 /// </summary> [ServiceContract] public interface IClientServer { /// <summary> /// 计算(测试方法) /// </summary> [OperationContract] double Calculate(TestData data); } }
实现:
using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Text; using System.Threading.Tasks; using DataStruct; namespace WCFServer { /// <summary> /// 服务实现 /// </summary> [ServiceBehavior()] public class ClientServer : IClientServer { /// <summary> /// 计算(测试方法) /// </summary> public double Calculate(TestData data) { return Math.Pow(data.X, data.Y); } } }
四、服务端启动服务:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.ServiceModel; using System.ServiceModel.Description; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Utils; using WCFServer; namespace 服务端 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { BackWork.Run(() => { OpenClientServer(); }, null, (ex) => { MessageBox.Show(ex.Message); }); } /// <summary> /// 启动服务 /// </summary> private void OpenClientServer() { NetNamedPipeBinding wsHttp = new NetNamedPipeBinding(); wsHttp.MaxBufferPoolSize = 524288; wsHttp.MaxReceivedMessageSize = 2147483647; wsHttp.ReaderQuotas.MaxArrayLength = 6553600; wsHttp.ReaderQuotas.MaxStringContentLength = 2147483647; wsHttp.ReaderQuotas.MaxBytesPerRead = 6553600; wsHttp.ReaderQuotas.MaxDepth = 6553600; wsHttp.ReaderQuotas.MaxNameTableCharCount = 6553600; wsHttp.CloseTimeout = new TimeSpan(0, 1, 0); wsHttp.OpenTimeout = new TimeSpan(0, 1, 0); wsHttp.ReceiveTimeout = new TimeSpan(0, 10, 0); wsHttp.SendTimeout = new TimeSpan(0, 10, 0); wsHttp.Security.Mode = NetNamedPipeSecurityMode.None; Uri baseAddress = new Uri("net.pipe://localhost/pipeName1"); ServiceHost host = new ServiceHost(typeof(ClientServer), baseAddress); ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); host.Description.Behaviors.Add(smb); ServiceBehaviorAttribute sba = host.Description.Behaviors.Find<ServiceBehaviorAttribute>(); sba.MaxItemsInObjectGraph = 2147483647; host.AddServiceEndpoint(typeof(IClientServer), wsHttp, ""); host.Open(); } } }
五、客户端数据实体类和服务接口类与服务端相同
六、客户端服务实现:
using DataStruct; using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.ServiceModel.Description; using System.Text; using System.Threading.Tasks; using WCFServer; namespace DataService { /// <summary> /// 服务实现 /// </summary> public class ClientServer : IClientServer { ChannelFactory<IClientServer> channelFactory; IClientServer proxy; public ClientServer() { CreateChannel(); } /// <summary> /// 创建连接客户终端WCF服务的通道 /// </summary> public void CreateChannel() { string url = "net.pipe://localhost/pipeName1"; NetNamedPipeBinding wsHttp = new NetNamedPipeBinding(); wsHttp.MaxBufferPoolSize = 524288; wsHttp.MaxReceivedMessageSize = 2147483647; wsHttp.ReaderQuotas.MaxArrayLength = 6553600; wsHttp.ReaderQuotas.MaxStringContentLength = 2147483647; wsHttp.ReaderQuotas.MaxBytesPerRead = 6553600; wsHttp.ReaderQuotas.MaxDepth = 6553600; wsHttp.ReaderQuotas.MaxNameTableCharCount = 6553600; wsHttp.SendTimeout = new TimeSpan(0, 10, 0); wsHttp.Security.Mode = NetNamedPipeSecurityMode.None; channelFactory = new ChannelFactory<IClientServer>(wsHttp, url); foreach (OperationDescription op in channelFactory.Endpoint.Contract.Operations) { DataContractSerializerOperationBehavior dataContractBehavior = op.Behaviors.Find<DataContractSerializerOperationBehavior>() as DataContractSerializerOperationBehavior; if (dataContractBehavior != null) { dataContractBehavior.MaxItemsInObjectGraph = 2147483647; } } } /// <summary> /// 计算(测试方法) /// </summary> public double Calculate(TestData data) { proxy = channelFactory.CreateChannel(); try { return proxy.Calculate(data); } catch (Exception ex) { throw ex; } finally { (proxy as ICommunicationObject).Close(); } } } }
七、客户端调用服务接口:
using DataService; using DataStruct; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Utils; using WCFServer; namespace 客户端 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //测试1 private void button1_Click(object sender, EventArgs e) { button1.Enabled = false; txtSum.Text = string.Empty; IClientServer client = new ClientServer(); double num1; double num2; double sum = 0; if (double.TryParse(txtNum1.Text, out num1) && double.TryParse(txtNum2.Text, out num2)) { DateTime dt = DateTime.Now; BackWork.Run(() => { sum = client.Calculate(new TestData(num1, num2)); }, () => { double time = DateTime.Now.Subtract(dt).TotalSeconds; txtTime.Text = time.ToString(); txtSum.Text = sum.ToString(); button1.Enabled = true; }, (ex) => { button1.Enabled = true; MessageBox.Show(ex.Message); }); } else { button1.Enabled = true; MessageBox.Show("请输入合法的数据"); } } //测试2 private void button2_Click(object sender, EventArgs e) { button2.Enabled = false; txtSum.Text = string.Empty; IClientServer client = new ClientServer(); double num1; double num2; double sum = 0; if (double.TryParse(txtNum1.Text, out num1) && double.TryParse(txtNum2.Text, out num2)) { DateTime dt = DateTime.Now; BackWork.Run(() => { for (int i = 0; i < 1000; i++) { sum = client.Calculate(new TestData(num1, num2)); } }, () => { double time = DateTime.Now.Subtract(dt).TotalSeconds; txtTime.Text = time.ToString(); txtSum.Text = sum.ToString(); button2.Enabled = true; }, (ex) => { button2.Enabled = true; MessageBox.Show(ex.Message); }); } else { button2.Enabled = true; MessageBox.Show("请输入合法的数据"); } } } }
八、工具类BackWork类:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; /** * 使用方法: BackWork.Run(() => //DoWork { }, () => //RunWorkerCompleted { }, (ex) => //错误处理 { }); */ namespace Utils { /// <summary> /// BackgroundWorker封装 /// 用于简化代码 /// </summary> public class BackWork { /// <summary> /// 执行 /// </summary> /// <param name="doWork">DoWork</param> /// <param name="workCompleted">RunWorkerCompleted</param> /// <param name="errorAction">错误处理</param> public static void Run(Action doWork, Action workCompleted, Action<Exception> errorAction) { bool isDoWorkError = false; Exception doWorkException = null; BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += (s, e) => { try { doWork(); } catch (Exception ex) { isDoWorkError = true; doWorkException = ex; } }; worker.RunWorkerCompleted += (s, e) => { if (!isDoWorkError) { try { if (workCompleted != null) workCompleted(); } catch (Exception ex) { errorAction(ex); } } else { errorAction(doWorkException); } }; worker.RunWorkerAsync(); } } }
九、效果图示:
以上这篇WCF实现进程间管道通信Demo分享就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持猪先飞。
相关文章
- 这篇文章主要为大家详细介绍了PC蓝牙通信C#代码实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 在.NET平台下创建C#串口通信程序,.NET 2.0提供了串口通信的功能,其命名空间是System.IO.Ports,创建C#串口通信程序的具体实现是如何的呢?让我们开始吧...2020-06-25
- 线程通信相信大家都不陌生了,但是你知道几种方法呢,本文主要介绍了android实现线程间通信的四种常见方式,分享给大家,需要的朋友们下面随着小编来一起学习学习吧...2021-05-13
- 这篇文章主要介绍了vue组件之间的通信,帮助大家更好的理解和学习前端的相关知识,感兴趣的朋友可以了解下...2020-08-30
- 本篇文章主要介绍了详解Vue 非父子组件通信方法(非Vuex),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 ...2017-05-27
- 本篇文章主要介绍了React组件通信详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2021-10-15
- 这篇文章主要介绍了React中组件之间通信的方式,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-07-27
- vue通过provide & inject两个关键字完成父组件向子孙组件直接传值,很像子类能够使用父类的属性一样方便。provide & inject一般用于多层之间的传值,两层之间还是使用props进行...2021-10-10
- 这篇文章主要介绍了C#串口通信实现方法,详细讲述了C#串口通信所涉及的数据接收与发送方法,以及相关的线程调用方法,是非常典型的应用,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了C#中使用UDP通信实例,非常实用的技巧,需要的朋友可以参考下...2020-06-25
- 本篇文章是对消息队列的应用进行了详细的分析介绍,需要的朋友参考下...2020-04-25
- 这篇文章主要介绍了Python基础之Socket通信原理,文中有非常详细的代码示例,对正在学习python基础的小伙伴们有非常好的帮助,需要的朋友可以参考下...2021-04-22
- 最简单粗暴的通信方式是 Nodejs调用一下 Python 脚本,本文详细介绍了Nodejs与Python 双向通信的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-07-17
- 这篇文章主要介绍了C#实现子窗体与父窗体通信方法,实例总结了常用的四种窗体通信方法,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 本文主要介绍了C#使用UdpClient类进行简单通信的实例,具有很好的参考价值,需要的朋友可以看下...2020-06-25
docker容器间跨宿主机通信-基于overlay的实现方法
这篇文章主要介绍了docker容器间跨宿主机通信-基于overlay的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-02-20- 本篇文章主要介绍了详解Vue 非父子组件通信,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2021-10-10
- 刚接触微信小程序,对里面的语法和属性还不怎么了解,最近正在努力学习中,下面这篇文章主要给大家介绍了微信小程序中多个页面传参通信的相关资料,是最近学习的一个内容总结,需要的朋友可以参考借鉴,下面来一起看看吧。...2017-05-09
- 本篇文章是对Visual C++进行串口通信编程进行了详细的分析介绍,需要的朋友参考下...2020-04-25
- 本篇文章主要介绍了学习 NodeJS 第八天:Socket 通讯实例,非常具有实用价值,需要的朋友可以参考下。 ...2016-12-31