本文介绍了我怎样才能实现与WCF的的ChannelFactory&LT客户端异步调用; T>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们正在编写一个客户端一个计算器Web服务使用WCF的的ChannelFactory。服务合同是由两个执行服务和客户端引用的第三组件的方式共享。下面是服务合同(其不能改变!)

Suppose we are writing a client for a Calculator web service using WCF's ChannelFactory. The service contract is shared by way of a third assembly referenced by both the implementation service and the client. Below is the service contract (which cannot be changed!)

public interface ICalculator 
{
    int Add(int x, int y);
}

本的ChannelFactory创建一个透明的代理对象,嘲笑的ICalculator服务合同,通过方法调用到RealProxy对象,然后直接发送下来的WCF通道堆栈。有没有一种方法来操纵WCF(在客户端只!)来自动曝光任务型服务业务,类似于VS-自动生成服务代理?

The ChannelFactory creates a transparent proxy object that "mocks" the ICalculator service contract, passing method calls to a RealProxy object which then sends the message down the WCF channel stack. Is there a way to manipulate WCF (on the client side ONLY!) to auto-expose Task-friendly service operations, similar to VS-auto-generated service proxies?

需要明确的是,我不希望修改我的服务是异步友好的反正。我希望我的客户端进行处理,同时等待通常阻断服务调用来完成。

To be clear, I'm not looking to modify my service to be async-friendly in anyway. I want my client to proceed processing while waiting for ordinarily blocking service calls to complete.

推荐答案

我做了一些探索使用ILSpy,发现两者的ChannelFactory和VS服务代理code都利用相同的code下的井盖产生为ICalculator透明代理。唯一的区别是,自动根code创建一个自定义的界面,反映了服务的合同,再加上任务异步方法,与指向回到真正的服务操作的URI OperationContract的属性。

I did some exploring using ILSpy and found that both ChannelFactory and VS' service proxy code both leverage the same code underneath the covers to generate a transparent proxy for ICalculator. The only difference is that the auto-gen code created a custom interface that mirrored the service's contract, plus Task async methods, with OperationContract attributes that pointed back to the "real" service operation URIs.

[ServiceContract]
public interface ICalculatorClient
{
    [ServiceOperation(Action="http://tempuri.org/ICalculator/Add")]
    Task<int> AddAsync(int x, int y);
}

因此​​,而不是使用包含原始服务的服务合同的组件,我创建了客户端code(这个名字只是为了消除歧义本次测试)我自己ICalculatorClient服务合同为特色所需的任务异步方法呼叫,只与一个操作属性指向的URI在服务器合同真正的服务操作所需ServiceOperation属性。

So, instead of using an assembly containing the original service's service contract, I created my own ICalculatorClient service contract in the client code (the name was just to eliminate ambiguity for this test) which featured the desired Task async method calls, only with the requisite ServiceOperation attributes with an Action property pointing to the URI for the "real" service operation on the server contract.

在运行时,所需的行为观察:我的客户code,同时等待来自服务器,它最终收到预期的响应进行。该服务是毫无收获,没有被修改,以满足客户的需求。客户是所有的聪明,在其与Web服务交互的细节获得控制。

At runtime, the desired behavior was observed: My client code proceeded while waiting on a response from the server, which it eventually received as expected. The service is none the wiser, not having been modified to address client needs. The client is all the smarter, gaining control over the details of its interaction with a web service.

这篇关于我怎样才能实现与WCF的的ChannelFactory&LT客户端异步调用; T&GT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 21:46