It is my understanding that every type (other than some primitives like int and string) used in a WCF ServiceContract need to be declared with ServiceKnownType attribute. But, I have build a custom object and it is transmitted accross my WCF service with no problem -- even though I have not added a ServiceKnownType for it. Will someone please explain why this works?
[ServiceContract(CallbackContract = typeof(IMyServiceCallback))]
public interface IMyService
{
[OperationContract]
List<MyObject> LoadMyObjects();
}
[DataContract]
public class MyObject
{
[DataMember]
private int batchID;
[DataMember]
private int fileID;
[DataMember]
private string fileName;
[DataMember]
private DateTime importStartTime;
// ...
}
No it is not correct. ServiceKnownType (or KnownTypeAttribute on data contract) is only needed for types used by the service but not specified in operation definition. In your case you have defined LoadMyObjects operation which uses MyObject class. Because the operation directly uses MyObject you don't have to add MyObject as ServicKnownType. But if you define MyObject2 derived from MyObject you will not be able to send that object from LoadMyObjects operation until you declare MyObject2 as ServiceKnownType.