Friday, August 20, 2010

WCF – Gotcha! DataContracts and Field Order

If you are trying to test a WCF service and you are attempting to manually call the operation (through a tool like SoapUI), then be aware that WCF expects the in-coming data to be in the order that the Data-Contract was setup.

If you attempt to reorder the data in what seems to be logical to you, then the field that is not in order will not be serialized and will get default data.

So pay attention the XSD and if the type is defined as:

<xs:complexType name="myData">
      <xs:sequence>
         <xs:element minOccurs="0" name="EndDate" type="xs:dateTime"/>
         <xs:element minOccurs="0" name="StartDate" type="xs:dateTime"/>
      </xs:sequence>
</xs:complexType>

Don’t try and send data where you reorder the StartDate and EndDate cause if you do, then StartDate will never get its value.

Note: this is important only when you use tools such as SoapUI that allow you to manually specify the outgoing data to the web-service. Normally serializers in the client will take care of the ordering and you will not have to worry about this issue.

No comments:

Post a Comment

Remember, if you want me to respond to your comment, then you need to use a Google/OpenID account to leave the comment.