WCF如何实现进程间管道通信
这篇文章主要为大家展示了“WCF如何实现进程间管道通信”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“WCF如何实现进程间管道通信”这篇文章吧。
从策划到设计制作,每一步都追求做到细腻,制作可持续发展的企业网站。为客户提供网站设计、成都网站设计、网站策划、网页设计、主机域名、虚拟空间、网络营销、VI设计、 网站改版、漏洞修补等服务。为客户提供更好的一站式互联网解决方案,以客户的口碑塑造优易品牌,携手广大客户,共同发展进步。
一、代码结构:
二、数据实体类:
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; namespace DataStruct { ////// 测试数据实体类 /// [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 { ////// 服务接口 /// [ServiceContract] public interface IClientServer { ////// 计算(测试方法) /// [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 { ////// 服务实现 /// [ServiceBehavior()] public class ClientServer : IClientServer { ////// 计算(测试方法) /// 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); }); } ////// 启动服务 /// 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(); 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 { ////// 服务实现 /// public class ClientServer : IClientServer { ChannelFactorychannelFactory; IClientServer proxy; public ClientServer() { CreateChannel(); } /// /// 创建连接客户终端WCF服务的通道 /// 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(wsHttp, url); foreach (OperationDescription op in channelFactory.Endpoint.Contract.Operations) { DataContractSerializerOperationBehavior dataContractBehavior = op.Behaviors.Find () as DataContractSerializerOperationBehavior; if (dataContractBehavior != null) { dataContractBehavior.MaxItemsInObjectGraph = 2147483647; } } } /// /// 计算(测试方法) /// 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 { ////// BackgroundWorker封装 /// 用于简化代码 /// public class BackWork { ////// 执行 /// /// DoWork /// RunWorkerCompleted /// 错误处理 public static void Run(Action doWork, Action workCompleted, ActionerrorAction) { 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如何实现进程间管道通信”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!
文章标题:WCF如何实现进程间管道通信
分享链接:http://ybzwz.com/article/gcedoj.html