简体   繁体   中英

Why is ServiceKnownType not needed for my object?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM