使用消息契约的 WCF 序列化枚举

发布于 2024-08-29 17:54:19 字数 1029 浏览 5 评论 0原文

我有一些使用消息合约的网络服务。可能值得一提的是,对于这些服务,我无法转向数据契约...

我的一个类型指定了一个属性,其类型恰好是枚举:

[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;
        }
    }

我的枚举定义为:

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

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

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

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

但是当我通过网络发送它时, RiskTypeCode 属性未序列化 - 即从输出中省略。

我需要用什么来装饰我的枚举/属性才能使其通过电线?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

药祭#氼 2024-09-05 17:54:19

使用DataContractEnumMember作为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();
        }

    }

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

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文