Why values in my WCF data contract were suddenly wrong...
        Posted  
        
            by mipsen
        on Geeks with Blogs
        
        See other posts from Geeks with Blogs
        
            or by mipsen
        
        
        
        Published on Sat, 06 Mar 2010 09:24:20 GMT
        Indexed on 
            2010/03/07
            23:28 UTC
        
        
        Read the original article
        Hit count: 336
        
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...
© Geeks with Blogs or respective owner