<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="Artech.DuplexServices.Services.CalculatorService">
<endpoint address="net.tcp://127.0.0.1:9999/CalculatorService"
binding="netTcpBinding" contract="Artech.DuplexServices.Contracts.ICalculator"></endpoint>
</service>
</services>
</system.serviceModel>
</configuration>

今天学习了WCF回调代码,可能的异常,注意点。

1.配置项的大小写要注意。

把配置项<endpoint address="net.tcp://127.0.0.1:9999/CalculatorService
      binding="netTcpbinding" contract="Artech.DuplexServices.Contracts.ICalculator"></endpoint>,红色部分没有注意大小写问题,导致了程序加载类异常。应该是binding="netTcpBinding"。

2.回调时不能关闭Tcp连接。

3.防止调用时的死锁。需要再契约上使用属性[OperationContract(IsOneWay=true)]。

源码地址:http://pan.baidu.com/s/1ntyP1ZN

框架图

原程序

Sevices

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Artech.DuplexServices.Contracts;
using System.ServiceModel; namespace Artech.DuplexServices.Services
{
public class CalculatorService : ICalculator
{
public void Add(double x, double y)
{
double result = x + y;
ICallBack callback = OperationContext.Current.GetCallbackChannel<ICallBack>();
callback.DispalayResult(x, y, result);
}
}
}

Contracts:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel; namespace Artech.DuplexServices.Contracts
{
[ServiceContract(Namespace="http://www.artech.com/",CallbackContract=typeof(ICallBack))]
public interface ICalculator
{
[OperationContract(IsOneWay=true)]
void Add(double x, double y);
}
} namespace Artech.DuplexServices.Contracts
{
public interface ICallBack
{
[OperationContract(IsOneWay = true)]
void DispalayResult(double x, double y, double result);
}
}

Hosting,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using Artech.DuplexServices.Contracts;
using Artech.DuplexServices.Services;
using System.ServiceModel.Channels; namespace Artech.DuplexServices.Hosting
{ class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
{
host.Open();
Console.Read();
} }
}
}

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="Artech.DuplexServices.Services.CalculatorService">
<endpoint address="net.tcp://127.0.0.1:9999/CalculatorService"
binding="netTcpBinding" contract="Artech.DuplexServices.Contracts.ICalculator"></endpoint>
</service>
</services>
</system.serviceModel>
</configuration>

ClientConsole

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Artech.DuplexServices.Contracts;
using System.ServiceModel;
using System.ServiceModel.Channels;
//system.serviceModel/bindings/netTcpbinding namespace Artech.DuplexServices.Clients
{
public class CalculateCallback : ICallBack
{
public void DispalayResult(double x, double y, double result)
{
Console.WriteLine("x+y={2} when x={0} and y={1}",x,y,result);
}
}
} namespace Artech.DuplexServices.Clients
{
class Program
{
static void Main(string[] args)
{
InstanceContext instanceContext = new InstanceContext(new CalculateCallback());
using (DuplexChannelFactory<ICalculator> ChannelFactory = new DuplexChannelFactory<ICalculator>(
instanceContext, "CalculatorService"))
{
ICalculator proxy = ChannelFactory.CreateChannel();
using (proxy as IDisposable)
{
proxy.Add(,);
Console.ReadLine();
}
} }
}
}

ClientConsole-AppConfig

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint name="CalculatorService" address="net.tcp://127.0.0.1:9999/CalculatorService" binding="netTcpBinding"
contract="Artech.DuplexServices.Contracts.ICalculator">
</endpoint>
</client>
</system.serviceModel>
</configuration>

WCF回调操作的更多相关文章

  1. 跟我一起学WCF(9)——WCF回调操作的实现

    一.引言 在上一篇文章中介绍了WCF对Session的支持,在这篇文章中将详细介绍WCF支持的操作.在WCF中,除了支持经典的请求/应答模式外,还提供了对单向操作.双向回调操作模式的支持,此外还有流操 ...

  2. 重温WCF之数单向通讯、双向通讯、回调操作(五)

    一.单向通讯单向操作不等同于异步操作,单向操作只是在发出调用的瞬间阻塞客户端,但如果发出多个单向调用,WCF会将请求调用放入到服务器端的队列中,并在某个时间进行执行.队列的存储个数有限,一旦发出的调用 ...

  3. [WCF编程]10.操作:回调操作

    一.回调操作概述 WCF支持服务将调用返回给它的客户端.在回调期间,许多方面都将颠倒过来:服务将成为客户端,客户端将编程服务.回调操作可以用在各种场景和应用程序中,但在涉及事件或者服务发生时间需要通知 ...

  4. WCF分布式开发步步为赢(10):请求应答(Request-Reply)、单向操作(One-Way)、回调操作(Call Back).

    WCF除了支持经典的请求应答(Request-Reply)模式外,还提供了什么操作调用模式,他们有什么不同以及我们如何在开发中使用这些操作调用模式.今天本节文章里会详细介绍.WCF分布式开发步步为赢( ...

  5. WCF加密操作(包括证书和证书+帐号密码)

    WCF作为.net三大组件之一,伟大之处不用多说,但是其加密配置对于我这样的萌新来说还是颇有难度,因此将几天来的研究成果共享出来,与各位共勉~ 首先声明我的开发环境,Win10创意者更新 + Visu ...

  6. WCF 回调中操作线程

    回调的类 [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant, UseSynchronizationContext = fals ...

  7. Wcf使用Net.Tcp做回调操作

    契约: [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples", SessionMode = Se ...

  8. WCF之操作重载

    服务契约的方法重载,会在装载宿主时,抛出异常. 解决是在操作契约上Name设置为不同值,但是生成的代理会把Name的名称作为方法的名称,不过我们可以手动的修改代理类,使得方法名与服务声明的名称一样. ...

  9. 【WCF】操作选择器

    在开始吹牛之前,先说说.net Core的事情. 你不能把.NET Core作为全新体系来学习,因为它也是.NET.关于.NET Core,老周并不打算写什么,因为你懂了.NET,就懂了.NET Co ...

随机推荐

  1. 微信小程序相关

    https://www.cnblogs.com/shenzikun1314/p/7805168.html

  2. php扩展开发-资源类型

    资源类型在内核中的结构 //zend_list.h typedef struct _zend_rsrc_list_entry { void *ptr; int type; int refcount; ...

  3. 使用python写一个最基本的mapreduce程序

    一个mapreduce程序大致分成三个部分,第一部分是mapper文件,第二个就是reducer文件,第三部分就是使用hadoop command 执行程序. 在这个过程中,困惑我最久的一个问题就是在 ...

  4. Python全栈day 05

    Python全栈day 05 一.数据类型补充 1. int py2和py3的2种区别 py2有int和long,int的取值范围为-2^31~2^31-1,超出范围自动转为long,长整型. py2 ...

  5. [C#]常用开源项目

    [转][C#]常用开源项目 本文来自:http://www.cnblogs.com/sunxuchu/p/6047589.html Json.NET http://www.newtonsoft.com ...

  6. easyPOI导出excel报错

    http-nio--exec- at :: - excel cell export error ,data is :com.jn.ssr.superrescue.web.qc.dto.Automati ...

  7. Android面试收集录14 Android进程间通信方式

    一.使用 Intent Activity,Service,Receiver 都支持在 Intent 中传递 Bundle 数据,而 Bundle 实现了 Parcelable 接口,可以在不同的进程间 ...

  8. linux下多线程断点下载工具-axel

    今天要下载一下14G左右的文件,用wget约10小时,后来发现linux下有个多线程支持断点续传的下载工具axel,试了一下,下载速度大大增加. 包地址:http://pkgs.repoforge.o ...

  9. 如何将现有的项目添加到远程的git库里面!

    我们经常都会遇到这样的场景,就是将本地的一个项目同步到网上远程的git库里面.最近也遇到这样的问题,发现网上很少人讲到这个问题,但是这个问题是很多程序员遇到的版本库管理的最早的拦路虎. 我的远程是ht ...

  10. 生成器 yield, next ,send

    重要的yield :相当于一个断层,我们再用next取拿出每一层重要的next :生成器查看装置,查看每一个断层重要的send :和next一样查看每一个段层,不过在查看第二个断层的时候,就可以对前面 ...