开发者

WCF Serialising Enums using Message Contracts

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(RiskTypeC开发者_如何学编程ode))]
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?


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();
        }

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜