Enum will not be serialized in WCF

I am having a problem converting enum to my WCF application.

I have a class Accountthat inherits an interface IAccountdefined 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; }
   }
}

A class Accountthat inherits from IAccountis 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 is Enumas follows:

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

}

Creating an object Accounton the client side and calling the decorated AddAccount method OperationContractserializes 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">MyEmail@gmail.com</EmailAddress>
            </account>
        </AddAccount>
    </soap:Body>
</soap:Envelope>

You will notice that the enumeration is Countrynot serializable.

I tried everything: from decorating an enumeration with DataContract and EnumMember and even explicitly setting the namespace in the attribute values ​​for DataContracts to uniformly set the values ​​for EnumMember attributes, but nothing works.

, - - , , .

UPDATE:

, WCF -, , [System.Xml.Serialization.XmlElementAttribute(IsNullable = true)], .

+4
2

, :

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

:

[ServiceContract]
[ServiceKnownType(typeof(Country))]
public interface IMyService {
// etc..
}
+8

Enum DataContract EnumMember. ServiceKnownType, .

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

, . , - WCF?.

+1

Source: https://habr.com/ru/post/1530874/


All Articles