I have a WCF service and have to expose an interface as datacontract. There are two derived classes from the interface. The WCF service will return an object of the derived type.The client will have to 开发者_JAVA技巧cast it as a derived class. Is that possible on the client side ?.What should be my approach.
If you have derived classes, you need to "advertise" those on the data contract:
[DataContract]
[KnownType(typeof(DerivedType1))]
[KnownType(typeof(DerivedType2))]
public class BaseType
{
......
}
or you can also specify these kind of relations on a service contract:
[ServiceKnownType(typeof(DerivedType1))]
[ServiceKnownType(typeof(DerivedType2))]
[ServiceContract()]
public interface IService
{
[OperationContract]
BaseType GetItems();
}
Check out the MSDN docs on Data Contract Known Types or Service Known Types for more background info.
精彩评论