What's the point of a DataContract in WCF?

前端 未结 6 1640
情歌与酒
情歌与酒 2020-12-07 14:07

VS.net creates a template when you create a WCF project.

It adds a class to the iService1.cs file:

// Use a data contract as illustrated in the sampl         


        
6条回答
  •  难免孤独
    2020-12-07 14:35

    Another interesting thing to notice, is if you decorate your code with DataContract, you have a lot of control about what the client can see and must send back to your service. For example:

    [DataContract]
    public class SampleClass
    {
        [DataMember(IsRequired=true)]
        public int MyRequiredProperty { get; set; }
    
        [DataMember]
        public int MyOptionalProperty { get; set; }
    
        public int MyInternalProperty { get; set; }
    }
    

    On the example above, you defined that when receiving data, you MUST have MyRequiredProperty, and you can have or not MyOptionalProperty. Also, the client will never see MyInternalProperty (this can be for example some property that helps with your logic internally, but you don't want it being exposed at the client level).

提交回复
热议问题