<?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. hosts 文件的位置及作用

    一.位置 1.Window系统位置 C:\Windows\System32\drivers\etc 2.Linux系统位置 /etc/hosts 二.作用 综述:Hosts文件中指定了域名和IP地址的 ...

  2. PPT入门学习笔记1:待修改

    一直被比人忽悠实在是累了,我可以接受自己的失误,但我接受不了别人一次又一次的坑我! 做PPT的原则是什么? 1.一个目标: "一个PPT只为一类人服务,针对不同的听众制作不同层次内容的PPT ...

  3. Codeforces Round #428 (Div. 2) C. dfs

    C. Journey time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  4. Android 做项目总结

    1.base 2.跳转可以用uihelper 3.activity和处理逻辑分开 4.userhelper保存管理用户登录信息 5.验证输入框的时候,接受的参数不要是String ,而是Edittex ...

  5. 直接插入排序&希尔排序

    1.直接插入排序 时间复杂度O(n2) 工作原理: 通过构建有序序列,对于未排序数据,在已排序的序列中,从后向前扫描,找到相应的位置并插入. 插入排序在实现上,在从后向前扫描的过程中,需要反复把已排序 ...

  6. 部署微软Nano Server的好处是什么?

    虚拟化对数据中心效率和硬件利用率产生了戏剧性的影响,但是接下来有关系统整合的主要责任落 在了操作系统的重量上.虚拟机通常运行企业级操作系统,比如Windows Server,但是Windows Ser ...

  7. Object Pascal中文手册 经典教程

    Object Pascal 参考手册 (Ver 0.1)ezdelphi@hotmail.com OverviewOverview(概述)Using object pascal(使用 object p ...

  8. USACO Section1.1 Broken Necklace 解题报告

    beads解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...

  9. sublime text基本配置备份

    sublime text基本配置备份: // Settings in here override those in "Default/Preferences.sublime-settings ...

  10. Synology DS213J 群晖NAS git server架设方法!

    最近单位购入一台Synology  DS213J用作数据存储. 本人打算将一些项目的源代码也放在上面,他本身的套件中心提供了SVN SERVER和GIT SERVER. 设置SVN SERVER非常简 ...