Search code examples
c#soapservice-referencewcftestclient

All Enums evaluates to 0 when sending a request to a WCF service from a WCF test client


I have a very strange issue, I am sending a request from a WCF Test Client, I want to test my soap service, all enums evaluates to Zero. The Enums look like this:

[DataContract]
[Flags]
public enum Gender
{
    [EnumMember]
    Female= 1,
    [EnumMember]
    Male= 2
}

After intense Googling I learnt that adding the attributes [DataContract], [Flags] and [EnumMember] should resolve the issue but still no luck

ErrorMessage:

System.ServiceModel.CommunicationException: 'There was an error while trying to serialize parameter http://tempuri.org/:methodCallRequest. The InnerException message was 'Enum value '0' is invalid for type 'Gender' and cannot be serialized. Ensure that the necessary enum values are present and are marked with EnumMemberAttribute attribute if the type has DataContractAttribute attribute.'. Please see InnerException for more details.'

InnerException:

SerializationException: Enum value '0' is invalid for type 'Gender' and cannot be serialized. Ensure that the necessary enum values are present and are marked with EnumMemberAttribute attribute if the type has DataContractAttribute attribute.


Solution

  • Adding the attribute [DataMember(IsRequired = true)] on the Gender Enum property and also adding [DataContract] attribute on the object header resolved the issue