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 学习笔记之双工实现的更多相关文章

  1. WCF学习笔记之事务编程

    WCF学习笔记之事务编程 一:WCF事务设置 事务提供一种机制将一个活动涉及的所有操作纳入到一个不可分割的执行单元: WCF通过System.ServiceModel.TransactionFlowA ...

  2. WCF学习笔记之传输安全

    WCF学习笔记之传输安全 最近学习[WCF全面解析]下册的知识,针对传输安全的内容做一个简单的记录,这边只是简单的记录一些要点:本文的内容均来自[WCF全面解析]下册: WCF的传输安全主要涉及认证. ...

  3. WCF 学习笔记之异常处理

    WCF 学习笔记之异常处理 1:WCF异常在配置文件 <configuration> <system.serviceModel> <behaviors> <s ...

  4. WCF学习笔记(2)——使用IIS承载WCF服务

    通过前面的笔记我们知道WCF服务是不能独立存在,必须“寄宿”于其他的应用程序中,承载WCF服务的应用程序我们称之为“宿主”.WCF的多种可选宿主,其中比较常见的就是承载于IIS服务中,在这里我们来学习 ...

  5. WCF学习笔记1--发布使用配置文件的服务

    关于WCF的入门网上资料很多,可以参考蒋金楠老师的博客http://www.cnblogs.com/artech/archive/2007/02/26/656901.html,我是从这篇博客开始学习的 ...

  6. WCF学习笔记之消息交换模式

    在WCF通信中,有三种消息交换模式,OneWay(单向模式), Request/Reponse(请求回复模式), Duplex(双工通信模式)这三种通信方式.下面对这三种消息交换模式进行讲解. 1. ...

  7. WCF学习笔记(1)——Hello WCF

    1.什么是WCF Windows Communication Foundation(WCF)是一个面向服务(SOA)的通讯框架,作为.NET Framework 3.0的重要组成部分于2006年正式发 ...

  8. WCF学习笔记(一):WCF简介

    转:http://www.cnblogs.com/wengyuli/archive/2009/11/04/1595693.html MSDN上关于WCF给出如下注解: 设计 Windows Commu ...

  9. WCF学习笔记(基于REST规则方式)

    一.WCF的定义 WCF是.NET 3.0后开始引入的新技术,意为基于windows平台的通讯服务. 首先在学习WCF之前,我们也知道他其实是加强版的一个面向服务(SOA)的框架技术. 如果熟悉Web ...

随机推荐

  1. CKPlayer的列表框的demo

    <!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...

  2. 在C#编程中玩转枚举,分享我的EnumHelper。

    在C#编程中玩转枚举,分享我的EnumHelper. 在软件开发过程中,我们经常会为特定的场景下的特定数据定义逻辑意义.比如在用户表中,我们可能会有一个用户状态字段,该字段为整形.如果该字段的值为1则 ...

  3. WCF常见问题(1) -- WebService/WCF Session Cookie

    原文:WCF常见问题(1) -- WebService/WCF Session Cookie 在.net 3.0推出WCF之前使用的WebService,有的应用有使用Session保持一些信息,在不 ...

  4. Nagios监控lvs服务

    1在lvs server上安装nrpe客户端: 1.1,rpm方式安装nrpe客户端 下载地址:http://download.csdn.net/detail/mchdba/7493875 [root ...

  5. EasyUI的后台界面

    EasyUI的后台界面搭建及极致重构 〇.前言 要了解一个东西长什么样,至少得让我们能看到,才能提出针对性的见解.所以,为了言之有物,而不是凭空漫谈,我们先从UI说起,后台管理页面的UI我们将使用应用 ...

  6. WimMaker 2.0 (2013.10) WIM制作工具

    WimMaker 2.0 (2013.10) WIM制作工具 可用于制作PE启动内核的Wim文件 说明: 因本软件使用.NET2.0制作,故主要用于制作WIM映像不用于备份还原系统(虽可用,但不专业, ...

  7. SQL点滴23—T-SQL中的除法

    原文:SQL点滴23-T-SQL中的除法 在T-SQL中没有除法运算,但是在T-SQL中可以实现类似除法的操作Divide.一般除法操作的结果一个列来自于被除关系表,剩下的来自除关系表.这里举一个例子 ...

  8. [译]Java 设计模式之外观

    (文章翻译自Java Design Pattern: Facade) 外观设计模式隐藏了任务的复杂性而只是提供了一个简单的接口.一个非常好的例子就是计算机的启动.当一个计算机启动的时候,它涉及CUP. ...

  9. C#编程实践—EventBroker简单实现

    前言 话说EventBroker这玩意已经不是什么新鲜货了,记得第一次接触这玩意是在进第二家公司的时候,公司产品基础架构层中集成了分布式消息中间件,在.net基础服务层中使用EventBroker的模 ...

  10. 制作iis自动安装包

    原文:制作iis自动安装包 MS 一直没有提供可独立安装的 IIS 安装包,Windows 的缺省安装没有安装它,通常要到控制面板的"添加/删除 Windows 程序"中去选择安装 ...