why an object of WCF service contract interface can be casted to IClientChannel

允我心安 提交于 2019-12-02 05:44:50

问题


In WCF Client Architecture, it says

When using the ChannelFactory class with a service contract interface, you must cast to the IClientChannel interface to explicitly open, close, or abort the channel

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. 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?

回答1:


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.

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

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

class Foo: IFoo {}

you can declare the object like this

IFoo instance = new 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.


So what is WCF doing?

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

IFoo instance = new ChannelFactory<IFoo>()

you'll get a runtime exception.

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

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.

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

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


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.



来源:https://stackoverflow.com/questions/26082494/why-an-object-of-wcf-service-contract-interface-can-be-casted-to-iclientchannel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!