2

I have a WCF Service (.NET4.0). Client is in .NET2.0. Enum values passed by the client into the service are always set with enum default value (which is the first enum member).

Is this an issue in .NET2.0 ? Are there any workarounds ?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
ideafountain
  • 276
  • 2
  • 3
  • 7
  • I found Specified field(bool) for every enum field. If I set these <>specified fields to true and then set values inside enum members, WCF service is able to get the exact passed value. If the Specified field is not set (default is false) then only default value of enum is recieved. Why this behaviour ? – ideafountain Jan 17 '12 at 22:56
  • This is because those properties are optional. The only way to indicate that they are present is with this separate field. If they were required, then the field would not be necessary. – John Saunders Jan 18 '12 at 00:00
  • Thanks John. I specified the IsRequired property for the datamember and the specificed fields disappeared and also the values set by client were recieved. This seems to be so for all datatypes which have default values in .NET like int, bool, enums. – ideafountain Jan 18 '12 at 05:11

1 Answers1

2

The <FieldName>Specified is a feature used by the XmlSerializer. I've been stung by this before. It seams everyone finds out about this feature in a similiarly painful manner.

As you say, if this is set to false then your field won't be serialised. Unfortunatly false is the default for a boolean field so it can very annoying if you forget to set it.

If you do not appreciate this feature then you should be able to just delete the <FieldName>Specified field without consequence. Then the field will always be serialized.

These fields only make a difference because you are using the XmlSerializer in .NET 2. In your .NET 4 WCF app you are using the DataContractSerializer which does not possess this feature.

The default value of the enum is not really received by WCF. Really nothing is received on the server for that enum. Consequently the enum is not set, so it remains the default value.

Buh Buh
  • 7,443
  • 1
  • 34
  • 61