WCF回调操作
<?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回调操作的更多相关文章
- 跟我一起学WCF(9)——WCF回调操作的实现
		
一.引言 在上一篇文章中介绍了WCF对Session的支持,在这篇文章中将详细介绍WCF支持的操作.在WCF中,除了支持经典的请求/应答模式外,还提供了对单向操作.双向回调操作模式的支持,此外还有流操 ...
 - 重温WCF之数单向通讯、双向通讯、回调操作(五)
		
一.单向通讯单向操作不等同于异步操作,单向操作只是在发出调用的瞬间阻塞客户端,但如果发出多个单向调用,WCF会将请求调用放入到服务器端的队列中,并在某个时间进行执行.队列的存储个数有限,一旦发出的调用 ...
 - [WCF编程]10.操作:回调操作
		
一.回调操作概述 WCF支持服务将调用返回给它的客户端.在回调期间,许多方面都将颠倒过来:服务将成为客户端,客户端将编程服务.回调操作可以用在各种场景和应用程序中,但在涉及事件或者服务发生时间需要通知 ...
 - WCF分布式开发步步为赢(10):请求应答(Request-Reply)、单向操作(One-Way)、回调操作(Call Back).
		
WCF除了支持经典的请求应答(Request-Reply)模式外,还提供了什么操作调用模式,他们有什么不同以及我们如何在开发中使用这些操作调用模式.今天本节文章里会详细介绍.WCF分布式开发步步为赢( ...
 - WCF加密操作(包括证书和证书+帐号密码)
		
WCF作为.net三大组件之一,伟大之处不用多说,但是其加密配置对于我这样的萌新来说还是颇有难度,因此将几天来的研究成果共享出来,与各位共勉~ 首先声明我的开发环境,Win10创意者更新 + Visu ...
 - WCF 回调中操作线程
		
回调的类 [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant, UseSynchronizationContext = fals ...
 - Wcf使用Net.Tcp做回调操作
		
契约: [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples", SessionMode = Se ...
 - WCF之操作重载
		
服务契约的方法重载,会在装载宿主时,抛出异常. 解决是在操作契约上Name设置为不同值,但是生成的代理会把Name的名称作为方法的名称,不过我们可以手动的修改代理类,使得方法名与服务声明的名称一样. ...
 - 【WCF】操作选择器
		
在开始吹牛之前,先说说.net Core的事情. 你不能把.NET Core作为全新体系来学习,因为它也是.NET.关于.NET Core,老周并不打算写什么,因为你懂了.NET,就懂了.NET Co ...
 
随机推荐
- 如何导入CSV数据 (python3.6.6区别于python2 环境)
			
1.python2环境下 2.python3.6.6环境下 如果用python2环境下的代码,在python3.6.6环境下编译会出现以下问题: 错误(1): SyntaxError:Missing ...
 - python——直方图均衡化
			
from PIL import Image from pylab import * from numpy import * def histeq(im,nbr_bins = 256): "& ...
 - GPIO基础知识
			
STM32 GPIO入门知识 GPIO是什么? 通用输入输出端口,可以做输入,也可以做输出.GPIO端口可通过程序配置成输入或输出. 引脚和GPIO的区别和联系 STM32的引脚中,有部分是做GPIO ...
 - TouTiao开源项目 分析笔记8 图解分析数据加载方式
			
1.整体构架 1.1.以一个段子页面为例,列出用到的主要的类,以图片的方式展示. 1.2.基础类 这里最基础的接口有: IBaseView<T>==>定义了5个方法. 然后最基础 ...
 - python ranndom模块及生成验证码
			
python的random模块用于生成随机数,下面介绍一下random模块的常用方法: 取随机小数: 数学计算 random.random() 用于生成一个0-1的随机浮点数 0<=n<1 ...
 - 途牛banner自动轮播
			
<!DOCTYPE html> <!--申明文档类型:html--> <html lang="en"> ...
 - CSS 一些基础知识(优先级、行内元素的一些属性、font-size单位)  怎样不加载图片
			
CSS大小写不敏感 选择器优先级如下所示: 在属性后面使用 !important 会覆盖页面内任何位置定义的元素样式. 作为style属性写在元素内的样式 id选择器 类选择器 标签选择器 通配符选择 ...
 - 《Cracking the Coding Interview》——第16章:线程与锁——题目6
			
2014-04-27 20:25 题目:关于java中标有synchronized的成员方法? 解法:这代表同一个对象实例的synchronized方法不能被多个线程同时调用.注意有这么多个地方都加粗 ...
 - HUAWEI TAG-AL00 找IMEI的过程
			
前几天,遇到一台华为机型,IMEI获取有问题,然后就找了一下. 以下是解决过程,权当记录一下,尽管为知笔记已经有备份了 :) 0x01: 起因 测试小哥发现,一台机型IMEI获取不全,有问题,拨号页面 ...
 - activiti并发多实例子流程任务处理
			
一直在搞工作流(activiti),总结一下关于工作流(activiti)中同时并发处理多个子流程的操作方法. 先说下我要实现的业务: 1.办公室发通知(在系统申报页面上,勾选科室,被选中的科室执行第 ...