WCF 学习笔记之双工实现
WCF 学习笔记之双工实现

其中 Client 和Service为控制台程序 Service.Interface为类库
首先了解契约Interface两个接口

using System.ServiceModel;
using System.ServiceModel.Channels;
namespace Artech.WcfServices.Service.Interface
{
[ServiceContract(Namespace = "http://www.artech.com/", CallbackContract = typeof(ICalculatorCallback))]
public interface ICalculator
{
[OperationContract(IsOneWay = true)]
void Add(double x, double y);
}
}

这边要注意两个地方:一个是明确回调查的接口,和设置让它为单向模式[CallbackContract = typeof(ICalculatorCallback)];IsOneWay = true
回调接口的代码如下:

using System.ServiceModel;
namespace Artech.WcfServices.Service.Interface
{
public interface ICalculatorCallback
{
[OperationContract(IsOneWay = true)]
void DisplayResult(double result, double x, double y);
}
}

此处也有两个地方要注意:回调契约没有定义[ServiceContact]的特性是因为在指定CallbackContract属性时就隐含对应的接口是个服务契约;此外同样也要设置为单向IsOneWay = true
接下来是服务实现层的代码:

using System.ServiceModel;
using Artech.WcfServices.Service.Interface;
using System.Threading;
namespace Artech.WcfServices.Service
{
public class CalculatorService : ICalculator
{
public void Add(double x, double y)
{
double result = x + y;
ICalculatorCallback callback = OperationContext.Current.GetCallbackChannel<ICalculatorCallback>();
callback.DisplayResult(result, x, y);
}
}
}

相对应的App.config配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="exposeExceptionDetail">
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Artech.WcfServices.Service.CalculatorService"
behaviorConfiguration="exposeExceptionDetail">
<endpoint address="net.tcp://127.0.0.1:3721/calculatorservice"
binding="netTcpBinding"
contract="Artech.WcfServices.Service.Interface.ICalculator"/>
</service>
</services>
</system.serviceModel>
</configuration>

关于绑定类型的选择必须明确是支持双向通信的类型才可以;比如NetTcpBinding,WSDualHttpBinding ;这边应该注意在采用WSDualHttpBinding时要把可靠会话打开,因为它是通过可靠会话维护两个HTTP通道之间的匹配;
例如:<reliableSession enabled="true"/>

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="reliableSessionBinding">
<reliableSession enabled="true"/>
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="exposeExceptionDetail">
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Artech.WcfServices.Service.CalculatorService"
behaviorConfiguration="exposeExceptionDetail">
<endpoint address="net.tcp://127.0.0.1:3721/calculatorservice"
binding="netTcpBinding"
bindingConfiguration="reliableSessionBinding"
contract="Artech.WcfServices.Service.Interface.ICalculator"/>
</service>
</services>
</system.serviceModel>
</configuration>

服务层还有一个代码就是宿主打开

using System.ServiceModel;
using Artech.WcfServices.Service.Interface;
using System.Threading;
using System.ServiceModel.Description;
namespace Artech.WcfServices.Service
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
{
host.Open();
Console.Read();
}
}
}
}

接下来是客户端的实现,要实现回调接口的

using Artech.WcfServices.Service.Interface;
namespace Artech.WcfServices.Client
{
public class CalculatorCallbackService : ICalculatorCallback
{
public void DisplayResult(double result, double x, double y)
{
Console.WriteLine("x + y = {2} when x = {0} and y = {1}", x, y, result);
}
}
}

以及相对应的配置文件内容:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint name ="calculatorservice"
address="net.tcp://127.0.0.1:3721/calculatorservice"
binding="netTcpBinding"
contract="Artech.WcfServices.Service.Interface.ICalculator"/>
</client>
</system.serviceModel>
</configuration>

以及运行的代码:

using System.ServiceModel;
using Artech.WcfServices.Service.Interface;
using System.ServiceModel.Channels;
using System.Threading;
namespace Artech.WcfServices.Client
{
class Program
{
static void Main(string[] args)
{
InstanceContext callback = new InstanceContext(new CalculatorCallbackService());
using (DuplexChannelFactory<ICalculator> channelFactory = new DuplexChannelFactory<ICalculator>(callback, "calculatorservice"))
{
ICalculator calculator = channelFactory.CreateChannel();
calculator.Add(1, 2);
}
Console.Read();
}
}
}

针对TCP协议要把服务器的相对应服务打开;或者可能会报错;

错误一:TransportManager 无法使用 NetTcpPortSharing 服务侦听提供的 URI: 无法启动服务,因为该服务已禁用。管理员运行 "sc.exe config NetTcpPortSharing start= demand" 可以将其启用 [NetTcpPortSharing 服务没有启动]
错误二:TransportManager 无法使用 NetTcpPortSharing 服务侦听提供的 URI: 服务侦听失败 [端口被占用,换个端口]
WCF 学习笔记之双工实现的更多相关文章
- WCF学习笔记之事务编程
WCF学习笔记之事务编程 一:WCF事务设置 事务提供一种机制将一个活动涉及的所有操作纳入到一个不可分割的执行单元: WCF通过System.ServiceModel.TransactionFlowA ...
- WCF学习笔记之传输安全
WCF学习笔记之传输安全 最近学习[WCF全面解析]下册的知识,针对传输安全的内容做一个简单的记录,这边只是简单的记录一些要点:本文的内容均来自[WCF全面解析]下册: WCF的传输安全主要涉及认证. ...
- WCF 学习笔记之异常处理
WCF 学习笔记之异常处理 1:WCF异常在配置文件 <configuration> <system.serviceModel> <behaviors> <s ...
- WCF学习笔记(2)——使用IIS承载WCF服务
通过前面的笔记我们知道WCF服务是不能独立存在,必须“寄宿”于其他的应用程序中,承载WCF服务的应用程序我们称之为“宿主”.WCF的多种可选宿主,其中比较常见的就是承载于IIS服务中,在这里我们来学习 ...
- WCF学习笔记1--发布使用配置文件的服务
关于WCF的入门网上资料很多,可以参考蒋金楠老师的博客http://www.cnblogs.com/artech/archive/2007/02/26/656901.html,我是从这篇博客开始学习的 ...
- WCF学习笔记之消息交换模式
在WCF通信中,有三种消息交换模式,OneWay(单向模式), Request/Reponse(请求回复模式), Duplex(双工通信模式)这三种通信方式.下面对这三种消息交换模式进行讲解. 1. ...
- WCF学习笔记(1)——Hello WCF
1.什么是WCF Windows Communication Foundation(WCF)是一个面向服务(SOA)的通讯框架,作为.NET Framework 3.0的重要组成部分于2006年正式发 ...
- WCF学习笔记(一):WCF简介
转:http://www.cnblogs.com/wengyuli/archive/2009/11/04/1595693.html MSDN上关于WCF给出如下注解: 设计 Windows Commu ...
- WCF学习笔记(基于REST规则方式)
一.WCF的定义 WCF是.NET 3.0后开始引入的新技术,意为基于windows平台的通讯服务. 首先在学习WCF之前,我们也知道他其实是加强版的一个面向服务(SOA)的框架技术. 如果熟悉Web ...
随机推荐
- 使用PHP生成PDF文档
原文:使用PHP生成PDF文档 实际工作中,我们要使用PHP动态的创建PDF文档,目前有许多开源的PHP创建PDF的类库,今天我给大家来介绍一款优秀的PDF库,它就是TCPDF,TCPDF是一个用于快 ...
- asp.net JSON(一)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 安卓MonkeyRunner源码分析之工作原理架构图及系列集合
花了点时间整理了下MonkeyRunner的工作原理图,请配合本人博客里面MonkeyRunner其他源码分析文章进行阅读.下面整理成相应系列列表方便大家阅读: MonkeyRunner源码分析之-谁 ...
- php中echo(),print(),print_r()用法
原文 php中echo(),print(),print_r()用法 从我对echo(),print(),print_r()这个函数的理解是echo可输入字符串变量常量,print与echo差不多,但p ...
- high performance program (SSE4.2 intrin instruction)
In file included from mm_lddqu.si128.c:2:0: /usr/local/lib/gcc/x86_64-redhat-linux/4.7.1/include/nmm ...
- leetcode第35题--Valid Sudoku
题目:Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 单极型ADC如何测量负电压?
最常用的方法是使用一个运放做成加法器将负电压抬到0V以上,如果这样的输出超过了最大输出电压那么再使用比例衰减就可以办到了. 参考下面的讨论: http://www.amobbs.com/thread- ...
- POJ2533 Longest Ordered Subsequence 【最长递增子序列】
Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 32192 Acc ...
- Centos中如何配置Texlive2013中文字体的问题
Centos中如何配置Texlive2013中文字体的问题: 第一步是下载你需要的字体,我从windows/fonts中拷贝的比较多,你只要复制你需要的字体即可. 注意只要文件扩展名为ttf的文件,t ...
- 利用sqlclr实现数据库服务器端数据加密解密
在公司中一同事用sqlclr写数据迁移自动化执行脚本,发现他在执行脚本时对数据进行了加密. 个人觉得利用sqlclr对数据进行加密是一个解决数据网络安全传输的不错的方案. 以下是一个小的案例: --- ...