3

I have some web services that use Message contracts. It's probably worth mentioning that for these services, I cannot shift to Data contracts...

One of my types specifies a property whose type happens to be an enum:

[SerializableAttribute()]
[MessageContract(IsWrapped = false)]
[KnownType(typeof(RiskTypeCode))]
public partial class RiskType : Lookup
{

    private RiskTypeCode codeField;

    /// <remarks/>
    [XmlElement(ElementName="code")]
    [MessageBodyMember]
    public RiskTypeCode Code
    {
        get
        {
            return this.codeField;
        }
        set
        {
            this.codeField = value;
        }
    }

e.t.c.

My enum is defined as:

[Serializable()]
[DataContract]
public enum RiskTypeCode
{

    /// <remarks/>
    [XmlEnumAttribute(Name = "THING1")]
    [EnumMember]
    THING1,

    /// <remarks/>
    [XmlEnumAttribute(Name="THING2")]
    [EnumMember]
    THING2,

    /// <remarks/>
    [XmlEnumAttribute(Name="THING3")]
    [EnumMember]
    THING3,
}

But when I send this across the wire, the RiskTypeCode property is not serialised - i.e. it's ommitted from the output.

What do I need to decorate my enum/property with to get it across the wire?

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
Robert
  • 1,487
  • 1
  • 14
  • 26
  • Hi @Kiquenet, sorry this was so long ago I don't remember how it was solved. I suspect we probably switched it for an integer, or possibly created an override for the stock serialiser. – Robert Feb 23 '14 at 22:58
  • @Kiquenet It looks useful, but I don't have the codebase anymore to verify whether this solves the problem or not. – Robert Oct 09 '14 at 19:35

1 Answers1

0

Using DataContract and EnumMember, for a MessageContract.

    [DataContract(Name = "ModoConsulta")]
    public enum ModoConsulta
    {
        [EnumMember()]
        Default = 0,
        [EnumMember()]
        RequestEV= 1,
        [EnumMember()]
        Server = 2
    }

    [MessageContract]
    public class QueryAdvancedReq : QueryReq
    {
        [MessageBodyMember]
        public DateTime? FromDate{ get; set; }

        [MessageBodyMember]
        public ModoConsulta Mode { get; set; }

        public override string ToString()
        {
            var str = new StringBuilder();
            str.Append(base.ToString());

            str.Append("FromDate : ");
            str.Append(FromDate== null ? "null" : FromDate.ToString());
            str.Append(", ");

            str.Append("Mode : ");
            str.Append(Mode == null ? "null" : Mode.ToString());
            str.Append(", ");

            return str.ToString();
        }

    }
Kiquenet
  • 14,494
  • 35
  • 148
  • 243