本文介绍了为什么可以将 WCF 服务契约接口的对象强制转换为 IClientChannel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WCF 客户端架构中,它说

将 ChannelFactory 类与服务协定接口一起使用时,您必须强制转换为 IClientChannel 接口以显式打开、关闭或中止通道

因此,您可以像这样创建和使用客户端通道对象:

So you create and use the client channel object like this:

var cf = new ChannelFactory<ICalculator>(binding, ea);

var channel = cf.CreateChannel();
double result = channel.Add(5.2, 3.5);
Console.WriteLine(channel.GetType()); // outputs ICalculator
Console.WriteLine(result);

((IClientChannel)channel).Close();

这里有两件事让我感到困惑.

There are two things that confused me here.

  1. CreateChannel的方法签名,以及第一个标准输出,表明channel变量是ICalculator类型,如何转换为IClientChannel?
  2. CreateChannel 返回的对象应该是实现任何接口的特定类型的对象,并且 channel.GetType() 应该返回那个确切的类型,为什么它返回接口类型?
  1. The method signaure of CreateChannel, as well as the first standard output, indicates that channel variable is of type ICalculator, how can it be converted to IClientChannel
  2. The object returned by CreateChannel should be certain typeof object that implements whatever interface(s), and channel.GetType() should return that exact type, why it returns an interface type?

推荐答案

我最近正在研究同样的事情,因为我在看到一些 WCF 源代码时试图弄清楚为什么我可以将我的频道转换为 ICommunicationObject代码在做.

I was recently was studying the same thing, because I was trying to figure out why I could cast my channel as an ICommunicationObject as I saw some of the WCF source code doing.

基本上.NET 有它在运行时需要的类型信息.

basically .NET has the type information it needs at runtime.

假设我们有一个实现接口的类

let's say we have a class that implements an interface

class Foo: IFoo {}

你可以像这样声明对象

IFoo instance = new Foo()

但您也可以直接将实例转换为 Foo()

but you can also cast the instance directly as a Foo()

Foo cast = (Foo)instance;

因为对象可以在运行时强制转换或转换为其类型.

Because an object can be cast or converted to its type at runtime.

WCF 在做什么?

为了好玩,尝试用你自己的接口实例化一个频道工厂

For fun, try to instantiate a channel factory with your own interface

IFoo instance = new ChannelFactory<IFoo>()

你会得到一个运行时异常.

you'll get a runtime exception.

"无效操作异常尝试获取 IFoo 的合同类型,但该类型不是 ServiceContract,也不是
继承 ServiceContract."

"InvalidOperationExceptionAttempted to get contract type for IFoo, but that type is not a ServiceContract, nor does it
inherit a ServiceContract."

当您使用 [ServiceContract] 属性修饰接口时,WCF 运行时会选择它并构建 WCF 构建频道所需的所有类.

When you decorate an interface with the [ServiceContract] attribute, the WCF runtime picks this up and builds all the classes that WCF needs to build your channel.

这包括 ICommunicationObject、IChannelFactory、IClientChannel 等接口

This includes interfaces like ICommunicationObject, IChannelFactory<TChannel>, IClientChannel, etc.

因此,尽管工厂方法将您的实例公开为 IFoo,但在运行时它也具有 WCF 需要运行的其他接口.

So although the factory method exposes your instance as an IFoo, at runtime it also has the other interfaces WCF needs to run.

最后,channel.GetType() 返回 ICalculatory 的原因是因为 GetType() 正在返回该对象的运行时类型.通过强制转换为 IClientChannel,您正在执行 WCF 保证会成功的类型转换.

Finally, the reason that channel.GetType() returns an ICalculatory, is because GetType() is returning the runtime type of that object. By casting to an IClientChannel, you are performing a type conversion that WCF guarantees will succeed.

这篇关于为什么可以将 WCF 服务契约接口的对象强制转换为 IClientChannel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 13:10