tags:

views:

19

answers:

1

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;
// ...
}
+1  A: 

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.

Ladislav Mrnka
Thanks. Has this been true all along, or is it a recent change to WCF? I guess I never realized.
TheSean
Yes this is true since the first version of WCF.
Ladislav Mrnka