一、服务端配置

<?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相关推荐

  1. 我的WCF之旅 (11): 再谈WCF的双向通讯-基于Http的双向通讯 V.S. 基于TCP的双向通讯...

    在一个基于面向服务的分布式环境中,借助一个标准的.平台无关的Communication Infrastructure,各个Service通过SOAP Message实现相互之间的交互.这个交互的过程实 ...

  2. WCF简单教程(6) 单向与双向通讯

    第六篇:单向与双向通讯 项目开发中我们时常会遇到需要异步调用的问题,有时忽略服务端的返回值,有时希望服务端在需要的时候回调,今天就来看看在WCF中如何实现. 先看不需要服务端返回值的单向调用,老规矩, ...

  3. C++的MFC 与 HTML 双向通讯

    C++中嵌入ie浏览器总结(1) - ie边框 及上下文菜单 最近项目中用html 来做界面,也就折腾了一下在wxwidget中嵌入浏览器的若干细节工作,mfc也基本是类似的,由于wxwidget中已 ...

  4. USART HMI智能串口屏与单片机双向通讯

    目录 ·HMI串口屏介绍 ·HMI串口屏开发实操 ·准备 ·界面认识 ·写命令 ·下载 ·HMI串口屏与单片机(stm32)双向通信 [ 功能实现: STM32控制串口屏对应的数值.文本完成相应变化 ...

  5. RS232实现串口双向通讯

    之前对接了一个pos机设备,设备使用了RS232进行通讯,现在也完成该需求并在线上运行使用当中了,此次对接实现的功能主要是应用程序向POS机设备发送应收金额和收费时间:至此想记录一下开发过程. 一.首 ...

  6. 智能工厂的IGT数据采集网关-PLC与数据库双向通讯的多种SQL语句配置

    IGT-DSER智能网关模块,支持各种PLC.智能仪表.远程IO与数据库之间双向通讯,既可以读取设备的数据上报到SQL数据库,也可以从数据库查询数据后写入到设备:数据库软件支持MySQL.SQLSer ...

  7. 安卓通讯之《蓝牙单片机通讯助手》②扫描设备、连接设备和双向通讯。

    前言 上篇文章我们介绍到了开发经典蓝牙和单片机通讯的过程,安卓通讯之<蓝牙单片机通讯助手>①集成工作 ,我们这里还要兼容最新的安卓6.0及以上的系统,因为从6.0以后的权限机制和以往的不一 ...

  8. 数据库网关-欧姆龙PLC与MySQL/SQLServer/PostgreSQL数据库实时双向通讯

    通过数据库智能网关IGT-DSER实现欧姆龙NJ501的PLC与服务器数据库双向通讯,网关支持MySQL.SQLServer.PostgreSQL,这里选择的是SQLServer. 首先,通过navi ...

  9. 蓝牙双向通讯【可自定义协议】SDK

    一.简介: 此文档主要是介绍蓝牙双向通讯sdk的调用方法以及蓝牙双向传输的核心功能逻辑,用户可以根据需要自定义协议进行蓝牙双向通讯 二.SDK接入步骤: 1: 增加sdk库依赖: a)libs文件夹下 ...

最新文章

  1. 六大主题报告,四大技术专题,AI开发者大会首日精华内容全回顾
  2. java 热替换_class卸载、热替换和Tomcat的热部署的分析
  3. golang中的条件变量
  4. VisualStudioAddIn2017.vsix的下载安装和使用
  5. js 根据固定位置获取经纬度--腾讯地图
  6. 数字化改革看“浙”里 CDEC2021中国数字智能生态大会杭州站举行
  7. 【Nginx】第一章 快速入门
  8. Flutter-ThemeData详解
  9. linux下gdb调试程序
  10. Easyui datagrid加载本地Json数据
  11. maven+springmvc出现:java.sql.SQLException: Unknown system variable 'query_cache_size'
  12. HTML5-jqUI----EasyUI
  13. Datawhale组队学习周报(第026周)
  14. java.lang.NoSuchMethodError问题解决
  15. 新一轮涨价潮来袭!英飞凌、安森美、ST、安世等交期长达52周
  16. dw自定义html模板,用DW编辑网站模板的方法
  17. 【绝对好用】java poi 导入、导出excel(支持xsl、xslx)
  18. 国产操作系统,路在何方?
  19. 阿里云ecs云服务器建立wordpress个人博客教程
  20. sql脚本之flush privileges的用法

热门文章

  1. vue实现接受后端传过来的zip数据,axios实现
  2. 机器学习物语(2):大数定理军团
  3. CF B. Working out
  4. c++primer 12.3.1文本查询程序
  5. pygame.rect中 Rect类 属性示意
  6. php文件包含绕过,文件包含漏洞(绕过姿势) | nmask's Blog
  7. SQL组合查询union
  8. STL-bitset源码解析
  9. 学习记录-Linux内核模块查看命令
  10. tbc服务器状态,魔兽怀旧服:TBC上线近俩月,玩家心态改变,开始涌入60级服务器...