Why values in my WCF data contract were suddenly wrong...
- by mipsen
A WCF Service I provided took a very simple data contract as parameter (containing one string and one int...) and had a very simple task to do. A .NET 3.5 client was created using the VS2008 feature "Add Service Reference". Everything worked as expected.
Then a slight change came in: The client was expected to run on machines with .NET 2.0 only. So we set the Target Framework to .NET 2.0, removed the references to System.ServiceModel, System.Runtime.Serialization and the ServiceReference and created a new Reference to the Service using the old "Add Web Reference" . A matter of 2 minutes.
When testing, the int value in the data contract arriving at the WCF Service suddenly was 0, instead of 38 as we expected.
What happened? When generating an old Web Reference on a WCF data contract an additional boolean field for each value-type field is created called [Fieldname]Specified (e.g. AgeSpecified) which defaults to "false". WCF inspects these boolean fields to determine if a value was provided for the value-type field. If the "Specified"-field is "false", WCF translates that to using the default-value of the value-type field. For int this is 0.
So we had to insert setting the "Specified"-field for the int-value to "true" and everything was fine again. That was what we forgot after setting the Framework-version to 2.0...