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 cast it as a derived class. Is that possible on the client side ?.What should be my approach.
A:
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.
marc_s
2010-06-07 16:41:32
@marc_s :In your example BaseType is a class and I am asking about interface. Is it possible with interface ?. I know knowntype works with classes but does it work with interface ?
Prashant
2010-06-07 19:50:35
@Prashant: no, WCF needs concrete classes - only those can be expressed in the XML schema that is needed for transferring data between the client and the server
marc_s
2010-06-07 20:58:05