none
Stange behavior on DataContract RRS feed

  • Question

  • Hi Everybody, 

     I have some strange issues in my WCF service, when I recieve a Datacontract all fields except some string values, which are not nullable. If I set the (IsRequired=true) on my DataMembers they are being transferred correctly, but this is wrong in my oppinion. 

    My MessageContract looks like this. 

     [MessageContract]
        public class MemberRequest 
        {
            [MessageBodyMember]
            public Member NewMember { get; set; }
            [MessageBodyMember]
            public int Test { get; set; }
    
        }

    My Datacontract looks like this. 

        [DataContract(Name="Member")]
        public class Member
        {
            [DataMember(IsRequired=true)]
            public NewMemberRequest Request { get; set; }
            
            [DataMember(IsRequired=true)]
            public int DepartmentNumber { get; set; }
        }

    My MemberRequest looks like this. 

     public class MemberRequest
        {
            [DataMember]
            public decimal? Code { get; set; }
            [DataMember]
            public string Remarks { get; set; }
            [DataMember]
            public bool? DanishCitizen { get; set; }
            [DataMember(IsRequired=true)]
            public bool? Mails { get; set; }
    }

    When I do a test through WCFTestClient and fill all my variables I on got the Mails field, rest of the nullabled properties are null when the service recieve them. 

    Any glues on whats going on?

    Best Regards

      Lars Folmer-Petersen

    Wednesday, September 16, 2015 12:32 PM

Answers

  • Hi Lars Folmer-Petersen,

    If you want to send null object to service. you can use Nullable<>. like below:

      [DataMember]
            public Nullable< DateTime> ECreate_Time { get; set; }

    If you set the IsRequired= true. the you send the value can not as null.

    For more information, please refer to the following atrticles:

    1.Nullable<T> Structure

    2.Nullable Types

    Best Regards,

    Vince Li

          

    Friday, September 25, 2015 8:33 AM

All replies

  • Hi Lars Folmer-Petersen,

    According to your description,  I check it in WCFTestClient. And I am not set any value.

    it running correctly. And I see it set null type as NullObject. When we use console application

    to test it. We must set a value to Mails, if we set IsRequired= true.

    Best Regards,

    Grady  

    Thursday, September 17, 2015 8:43 AM
    Moderator
  • Hi Grady, 

    But is it correctly understound, that for transferring a nullable object I have to set either IsRequired =true or use the [x]Specified on the property to get it sent over to the service?

    Best Regards

     Lars 

    Thursday, September 17, 2015 8:48 AM
  • Hi Lars Folmer-Petersen,

    If you want to transferring a nullable object , you needn't to set IsRequired= true, in your code.

    Best Regards,

    Vince Li

    Monday, September 21, 2015 7:29 AM
  • Hi Vince, 

     If I don't have to set IsRequired = true on eg. DateTime? startTime how do I then get the value transferred. 

     Because if I don't set anything like the IsRequired og startTimeSpecified the field will not being transferred is there something wrong with my WCF Service then?

    Best Regards 

    Lars 

    Monday, September 21, 2015 7:37 AM
  • Hi Lars Folmer-Petersen,

    If you want to send null object to service. you can use Nullable<>. like below:

      [DataMember]
            public Nullable< DateTime> ECreate_Time { get; set; }

    If you set the IsRequired= true. the you send the value can not as null.

    For more information, please refer to the following atrticles:

    1.Nullable<T> Structure

    2.Nullable Types

    Best Regards,

    Vince Li

          

    Friday, September 25, 2015 8:33 AM