Duplex Services from msdn

A duplex service contract is a message exchange pattern in which both endpoints can send messages to the other independently.
A duplex service, therefore, can send messages back to the client endpoint, providing event-like behavior.

Duplex communication occurs when a client connects to a service and provides the service with a channel on which the service can send messages back to the client.
Note that the event-like behavior of duplex services only works within a session.

To create a duplex contract you create a pair of interfaces.
The first is the service contract interface that describes the operations that a client can invoke.
That service contract must specify a callback contract in the ServiceContractAttribute.CallbackContract property.
The callback contract is the interface that defines the operations that the service can call on the client endpoint.
A duplex contract does not require a session, although the system-provided duplex bindings make use of them.

The following is an example of a duplex contract.

public interface ICalculatorDuplexCallback
{
[OperationContract(IsOneWay = true)]
void Equals(double result);
[OperationContract(IsOneWay = true)]
void Equation(string eqn);
}
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples", SessionMode = SessionMode.Required,
CallbackContract = typeof(ICalculatorDuplexCallback))]
public interface ICalculatorDuplex
{
[OperationContract(IsOneWay = true)]
void Clear();
[OperationContract(IsOneWay = true)]
void AddTo(double n);
[OperationContract(IsOneWay = true)]
void SubtractFrom(double n);
[OperationContract(IsOneWay = true)]
void MultiplyBy(double n);
[OperationContract(IsOneWay = true)]
void DivideBy(double n);
}

The CalculatorService class implements the primary ICalculatorDuplex interface.
The service uses the PerSession instance mode to maintain the result for each session.
A private property named Callback accesses the callback channel to the client.
The service uses the callback for sending messages back to the client through the callback interface, as shown in the following sample code.

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class CalculatorService : ICalculatorDuplex
{
double result = 0.0D;
string equation; public CalculatorService()
{
equation = result.ToString();
} public void Clear()
{
Callback.Equation(equation + " = " + result.ToString());
equation = result.ToString();
} public void AddTo(double n)
{
result += n;
equation += " + " + n.ToString();
Callback.Equals(result);
} public void SubtractFrom(double n)
{
result -= n;
equation += " - " + n.ToString();
Callback.Equals(result);
} public void MultiplyBy(double n)
{
result *= n;
equation += " * " + n.ToString();
Callback.Equals(result);
} public void DivideBy(double n)
{
result /= n;
equation += " / " + n.ToString();
Callback.Equals(result);
} ICalculatorDuplexCallback Callback
{
get
{
return OperationContext.Current.GetCallbackChannel<ICalculatorDuplexCallback>();
}
}
}

The client must provide a class that implements the callback interface of the duplex contract, for receiving messages from the service.
The following sample code shows a CallbackHandler class that implements the ICalculatorDuplexCallback interface.

public class CallbackHandler : ICalculatorDuplexCallback
{
public void Equals(double result)
{
Console.WriteLine("Equals({0})", result);
} public void Equation(string eqn)
{
Console.WriteLine("Equation({0})", eqn);
}
}

The WCF client that is generated for a duplex contract requires a InstanceContext class to be provided upon construction.
This InstanceContext class is used as the site for an object that implements the callback interface and handles messages that are sent back from the service.
An InstanceContext class is constructed with an instance of the CallbackHandler class.
This object handles messages sent from the service to the client on the callback interface.

// Construct InstanceContext to handle messages on callback interface
InstanceContext instanceContext = new InstanceContext(new CallbackHandler()); // Create a client
CalculatorDuplexClient client = new CalculatorDuplexClient(instanceContext);

The configuration for the service must be set up to provide a binding that supports both session communication and duplex communication.
The wsDualHttpBinding element supports session communication and allows duplex communication by providing dual HTTP connections, one for each direction.

On the client, you must configure an address that the server can use to connect to the client, as shown in the following sample configuration.

Note:
Non-duplex clients that fail to authenticate using a secure conversation typically throw a MessageSecurityException.
However, if a duplex client that uses a secure conversation fails to authenticate, the client receives a TimeoutException instead.

If you create a client/service using the WSHttpBinding element and you do not include the client callback endpoint, you will receive the following error.
HTTP could not register URL
htp://+:80/Temporary_Listen_Addresses/<guid> because TCP port 80 is being used by another application.

The following sample code shows how to specify the client endpoint address in code.

WSDualHttpBinding binding = new WSDualHttpBinding();
EndpointAddress endptadr = new EndpointAddress("http://localhost:12000/DuplexTestUsingCode/Server");
binding.ClientBaseAddress = new Uri("http://localhost:8000/DuplexTestUsingCode/Client/");

The following sample code shows how to specify the client endpoint address in configuration.

<client>
<endpoint name ="ServerEndpoint"
address="http://localhost:12000/DuplexTestUsingConfig/Server"
bindingConfiguration="WSDualHttpBinding_IDuplexTest"
binding="wsDualHttpBinding"
contract="IDuplexTest" />
</client>
<bindings>
<wsDualHttpBinding>
<binding name="WSDualHttpBinding_IDuplexTest"
clientBaseAddress="http://localhost:8000/myClient/" >
<security mode="None"/>
</binding>
</wsDualHttpBinding>
</bindings>

Caution
The duplex model does not automatically detect when a service or client closes its channel.
So if a client unexpectedly terminates, by default the service will not be notified, or if a client unexpectedly terminates, the service will not be notified.
Clients and services can implement their own protocol to notify each other if they so choose.

Duplex Services (Msdn)的更多相关文章

  1. Windows HTTP Services

    原文:https://msdn.microsoft.com/zh-cn/library/windows/desktop/aa384273(v=vs.85).aspx Purpose (目的) Micr ...

  2. 当程序以Windows Services形式启动时当前路径不对

    当程序以Windows Services形式启动时当前路径不对 @(操作系统)[博客|dotNet] 很多时候我们需要将我们的程序写成利用Windows服务的形式来让它能够自启动.今天遇到一个问题,当 ...

  3. SharePoint 2013 Excel Services REST API介绍

    前言:Excel Services 中的 REST API 是 Microsoft SharePoint Server 2010 的新增项.利用 REST API,可通过 URL 直接访问工作簿部件或 ...

  4. SharePoint 2013 Excel Services ECMAScript 示例之明日限行

    前言:最近遇到一个“明日限行”的功能,北京的交通啊,这个不在今天讨论范围内,暂不吐槽,想想代码开发,还要写WebPart部署,很麻烦,而且部署服务器,需要领导审批,想绕过这个麻烦事儿,就想到客户端了, ...

  5. (转载)SQL Reporting Services (Expression Examples)

    https://msdn.microsoft.com/en-us/library/ms157328(v=SQL.100).aspx Expressions are used frequently in ...

  6. Configure Security Settings for Remote Desktop(RDP) Services Connections

    catalogue . Configure Server Authentication and Encryption Levels . Configure Network Level Authenti ...

  7. 【HOW】如何通过URL给Reporting Services报表传递参数

    [本地模式Reporting Services] 参见官方文档:http://msdn.microsoft.com/en-us/library/ms154042.aspx 示例:http://serv ...

  8. 充分利用 SQL Server Reporting Services 图表

    最近在查SSRS的一些文章,看到MSDN在有一篇不错的文章,许多图表设置都有说明,共享给大家.. 其中有说明在SSRS中如果去写条件表达写和报表属性中的“自定义代码”,文章相对比较长,需要大家耐心的查 ...

  9. User Word Automation Services and Open XML SDK to generate word files in SharePoint2010

    SharePoint 2010 has established a new service called "Word Automation Services" to operate ...

随机推荐

  1. Windows2012中安装域控(DC) + SQL Server 2014 + TFS 2015

    安装域控(DC) 修改计算机名 修改固定IP 添加角色 选择“Role-based or feature-based installation” 选择本机 选择“Active Directory Do ...

  2. System Operations on AWS - Lab 1W - Creating EC2 (Windows)

    1. 创建CommandHost实例,登录到CommandHost,通过AWS CLI创建WebServer实例. 1.1 为CommandHost实例创建一个IAM角色 1.2 创建CommandH ...

  3. Windows PowerShell:管理服务器

    一.概述 Cmdlets 用于服务器的管理方面主要体现在4个方面:服务.日志.进程.服务器管理器. 1.服务 •  Get-Service.查看某个服务的属性. •  New-Service.创建一个 ...

  4. enable cors in spring mvc with swagger

    a. In controller add @CrossOrigin(origins = "http://localhost:8080") b. In mvc-servlet.xml ...

  5. PHP能得到你是从什么页面过来的,r…

    在开发web程序的时候,有时我们需要得到用户是从什么页面连过来的,这就用到了referer. 它是http协议,所以任何能开发web程序的语言都可以实现,比如jsp中是: request.getHea ...

  6. asp IIS部署An error occurred on the server when processing the URL错误提示解决

    An error occurred on the server when processing the URL. Please contact the system administrator.If ...

  7. 关于word-break,word-wrap换行

    目前项目中有一些流程日志需要动态显示到页面上,实现方法是ajax动态获取附加到<span></span>标签上,然后设置word-break:break-all样式使其自动换行 ...

  8. 继续(3n+1)猜想

    卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数.例如对n=3进行验证的时候, ...

  9. java_设计模式_迭代器模式_Iterator Pattern(2016-08-12)

    迭代子(Iterator)模式又叫游标(Cursor)模式,是对象的行为模式. 定义:提供一种方法访问一个容器对象中各个元素,而又不暴露该对象的内部细节. 类型:行为类模式 类图: 如果要问java中 ...

  10. VMware虚拟机中如何安装VMWare-Tools详解

    VMware虚拟机中如何安装VMWare-Tools详解 好处:可以支持图形界面,可以支持共享文件功能等 VMware虚拟机中如何配置显 VMware作为一款虚拟机利器,很多人都利用它来实现Linux ...