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

前端 未结 1 804
野的像风
野的像风 2021-01-21 10:14

In WCF Client Architecture, it says

When using the ChannelFactory class with a service contract interface, you must cast to the IClientChannel interface t

相关标签:
1条回答
  • 2021-01-21 10:55

    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.

    0 讨论(0)
提交回复
热议问题