Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum not getting serialized in WCF

I'm having a problem with an enum getting serialized in my WCF application.

I have a class Account which inherits from an interface IAccount defined as follows:

namespace MyNamespace
{
   public interface IAccount
   {
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string EmailAddress { get; set; }
      public Country Country { get; set; }
   }
}

The Account class which inherits from IAccount is defined as:

namespace MyNamespace
{
   [DataContract]
   public Account : IAccount
   {
    [DataMember]          
    public string FirstName { get; set; }

    [DataMember] 
    public string LastName { get; set; }

    [DataMember] 
    public string EmailAddress { get; set; }

    [DataMember] 
    public Country Country { get; set; }
   }
}

}

The Country Enum is as follows:

namespace MyNamespace
{
  public enum Country
  {
    America = 1,
    England = 2,
    France = 3
  }

}

Creating an Account object on the client side and calling the method AddAccount decorated with the OperationContract serializes the Account object as:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <AddAccount xmlns="http://tempuri.org/">
            <account>
                <FirstName xmlns="http://schemas.datacontract.org/2004/07/MyNamespace">MyFirstName</FirstName>
                <LastName xmlns="http://schemas.datacontract.org/2004/07/MyNamespace">MyLastName</LastName>
                <EmailAddress xmlns="http://schemas.datacontract.org/2004/07/MyNamespace">[email protected]</EmailAddress>
            </account>
        </AddAccount>
    </soap:Body>
</soap:Envelope>

You will notice that the Country enum is not getting serialized.

I've tried everything from decorating the enum with DataContract and EnumMember and even explicitly setting the namespace in the attribute values for the DataContracts to even setting Values for the EnumMember attributes but nothing seems to work.

I don't know whether i'm going nuts and missing out something really obvious or is there something else that I would need to do that's not part of the code above.

UPDATE:

Looking at the proxy class that was generated on the client side after adding the WCF application as a web reference I noticed that the enum is not decorated with [System.Xml.Serialization.XmlElementAttribute(IsNullable = true)] like the other properties.

like image 405
Draco Avatar asked Dec 25 '22 12:12

Draco


1 Answers

You need to decorate your enum with some contract information like:

  [DataContract]
  public enum Country
  {
    [EnumMember]
    America = 1,
    [EnumMember]
    England = 2,
    [EnumMember]
    France = 3
  }

Then on your service contract add:

[ServiceContract]
[ServiceKnownType(typeof(Country))]
public interface IMyService {
// etc..
}
like image 57
shenku Avatar answered Jan 03 '23 05:01

shenku