WCF:在运行时在常规服务和双工服务合同之间进行选择

时间:2012-12-28 18:14:22

标签: wcf generics wcf-client

我正在编写WCF客户端/服务。该服务可以执行一些长时间的操作,因此我添加了一个回调契约IProgressCallback。我正在开发的系统必须在各种环境中运行,所以我怀疑我会遇到一个无法打开回调通道的环境(我可能在这里错了)。

所以要小心我已经定义了这样的操作合同。

[ServiceContract()]
interface IContract
{ ... }

[ServiceContract(CallbackContract = typeof(IProgress))]
interface IDuplexContract : IContract
{ ... }

这在服务器端很有用。我可以轻松地将服务配置为使用任一合同。

然而问题出现在客户端。我手动定义了2个代理

public class ContractProxy 
   : ClientBase<IContract>, IContract
{ ... }

public class DuplexContractProxy 
   : DuplexClientBase<IDuplexContract>, IDuplexContract 
{ ... }

此外,两个代理都可以正常工作。

现在我有一个代理工厂负责创建正确的代理。它可以很容易地找出要实例化的代理,但是当我尝试返回代理时出现问题。

用户需要获得至少IContractICommunicationObject的对象,但我无法找到返回的内容。我试过以下内容:

IContract CreateProxy(...)  // The user lacks access to Open, Abort, Close, etc.
ClientBase<IContract> CreateProxy(...)  // DuplexClientBase derives from ClientBase but the generic parameter is different and it isn't covariant so this cannot be done.

// First define a generic interface and then make both proxies implement it.
IProxy<TContract> : TContract, ICommunicationObject  // TContract cannot be restricted to an interface so I cannot make IProxy derive from TContract

目前作为一种解决方法,我只是返回IContract接口,但是每个使用代理的人都必须首先将其转换为ICommunicationObject以打开代理等。

这里有没有人有更好的方法可以做到这一点,或者我只是过于担心双工通信在某些环境中可能无效?

0 个答案:

没有答案