WCF contract returning interface could cause serialization issue?

前端 未结 2 893
滥情空心
滥情空心 2020-12-15 13:06

I am trying to define a WCF contract that returns an interface, something like below:

[ServiceContract]
public interface IMyContracts
{
    [OperationContrac         


        
相关标签:
2条回答
  • 2020-12-15 13:14

    AFAIK, the problem is not with serialization but with the fact that you're returning abstract entity (an interface). Abstraction is an OO concept, not SOA concept. So, your client's wcf stack may not know what to do with the class behind the interface. What if the client does not know the class behind the interface. Client's WCF stack must deserialize it, and to do it, it must know the class.

    So, you must make the class(es) behind the interface part of your contract, via KnownTypeAttribute.

    You may also use ServiceKnownTypeAttribute class that seems to be more flexible. Still, remember that the client must know the type, or you'll get an exception.

    0 讨论(0)
  • 2020-12-15 13:23

    In this post, I go into details of how to make WCF work for receiving and returning derived classes and interfaces.

    http://codeonaboat.wordpress.com/2010/03/01/serializing-and-deserializing-derived-types-or-interfaces-in-wcf/

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