日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

WCF双向通讯netTCP

發布時間:2024/4/17 编程问答 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WCF双向通讯netTCP 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

一、服務端配置

<?xml version="1.0" encoding="utf-8" ?> <configuration><system.serviceModel><behaviors><serviceBehaviors><behavior name="MyBehavior"><serviceMetadata httpGetEnabled="true" /><serviceDebug includeExceptionDetailInFaults="false" /></behavior></serviceBehaviors><endpointBehaviors><behavior name="webBehavior"><webHttp /></behavior></endpointBehaviors></behaviors><services><service name="PayCommunicationWcf.Server.AliPay" ><endpoint address="" binding="netTcpBinding" contract="PayCommunicationWcf.Interface.IPay"><identity><!-- <dns value="localhost" />--></identity></endpoint><!-- <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />--> <host><baseAddresses><add baseAddress="net.tcp://localhost:57000/sessionservice" /></baseAddresses></host></service></services><bindings><webHttpBinding><binding name="ApiExportBinding" maxReceivedMessageSize="10485760"maxBufferPoolSize="10485760" maxBufferSize="10485760" closeTimeout="00:03:00"openTimeout="00:03:00" receiveTimeout="00:10:00" sendTimeout="00:03:00"><readerQuotas maxDepth="32" maxStringContentLength="10485760"maxArrayLength="10485760" maxBytesPerRead="10485760" /><security mode="None" /></binding></webHttpBinding></bindings></system.serviceModel> </configuration> View Code

二、服務端服務契約和服務實現

?1、服務實現

using System; using PayCommunicationWcf.Interface; using PayCommunicationWcf.Model; using PurClient.Logging;namespace PayCommunicationWcf.Server {public class PayCallBack : IPayCallBack{public void SendResultOfPay(PayResultResponse result){Logger.GetInstance().Info(string.Format("已經接收到服務端發來的支付結果消息,結果碼:{0},結果信息:{1}。",result.Resultcode, result.Msg));}} } View Code

2、服務契約

?服務契約

using System.ServiceModel; using System.ServiceModel.Web; using PayCommunicationWcf.Model;namespace PayCommunicationWcf.Interface {[ServiceContract(CallbackContract = typeof(IPayCallBack))]public interface IPay{/* [WebInvoke(UriTemplate = "PayRequestTest", BodyStyle = WebMessageBodyStyle.Bare,Method = "*",RequestFormat =WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)]*/ [OperationContract]PayResultResponse PayRequestTest(PayRequestInfo payData);/* [WebInvoke(UriTemplate = "PayRequest?payData={payData}", BodyStyle = WebMessageBodyStyle.Bare, Method = "*", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]*/ [OperationContract]string PayRequest(string payData);} } View Code

回調服務契約,在客戶端中實現

using System.ServiceModel; using PayCommunicationWcf.Model;namespace PayCommunicationWcf.Interface {[ServiceContract]public interface IPayCallBack{[OperationContract(IsOneWay = true)]void SendResultOfPay(PayResultResponse result);} } View Code

3、服務注冊

using System.ServiceModel;namespace PayCommunicationWcf.Server {public static class ServiceRegister{/// <summary>/// 通過反射注冊服務/// </summary>public static void RegisterAllService(){ServiceHost host = new ServiceHost(typeof(AliPay));if (host.State != CommunicationState.Opening)host.Open();}} } View Code

三 、客戶端配置

<?xml version="1.0" encoding="utf-8" ?> <configuration><system.serviceModel><client><endpoint address="net.tcp://182.150.28.182:57000/sessionservice" binding="netTcpBinding"bindingConfiguration="" contract="PayCommunicationWcf.Interface.IPay"name="sessionservice" /></client><bindings><netTcpBinding><binding><security mode="None"></security></binding></netTcpBinding></bindings></system.serviceModel> </configuration> View Code

四、客戶端實現

1、回調契約實現

using System; using PayCommunicationWcf.Interface; using PayCommunicationWcf.Model; using PurClient.Logging;namespace PayCommunicationWcf.Server {public class PayCallBack : IPayCallBack{public void SendResultOfPay(PayResultResponse result){Logger.GetInstance().Info(string.Format("已經接收到服務端發來的支付結果消息,結果碼:{0},結果信息:{1}。",result.Resultcode, result.Msg));}} } View Code

2、客戶端創建

private static IPay Channel{ get; set; }private static IPayCallBack Callback{ get; set; }private void FormMain_Load(object sender, EventArgs e){var logger = Logger.GetInstance(); ;logger.DelShowUiLogEvent += Loger_DelShowUiLogEvent;Callback = new PayCallBack();Channel = new DuplexChannelFactory<IPay>(Callback, "sessionservice").CreateChannel();// ServiceRegister.RegisterAllService();logger.Info("初始化完成。");} View Code

?五、整解決方案源碼

點擊下載

轉載于:https://www.cnblogs.com/musexiaoluo/p/7237045.html

總結

以上是生活随笔為你收集整理的WCF双向通讯netTCP的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。