문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top