【问题标题】:Why are Enumeration Value annotations missing for Zero based indexes in my WSDL为什么我的 WSDL 中基于零的索引缺少枚举值注释
【发布时间】:2015-08-21 15:51:27
【问题描述】:

我的 WFC 项目 WSDL 缺少基于零的索引的 EnumerationValue 注释。如果说 JSON 为付款方式传入 0 或 1,则会在转换期间对其他系统造成问题。

我已经像这样定义了我的 .net 类的枚举:

[DataContract(Name = "PaymentMethod")]
public enum PaymentMethod
{
    [EnumMember]
    Mastercard = 0,
    [EnumMember]
    Visa = 1
}

[DataContract(Name = "RequestType")]
public enum RequestType
{
    [EnumMember]
    New = 1,
    [EnumMember]
    Reload = 2
}

但是生成的 WSDL 看起来像这样:

<xs:simpleType name="PaymentMethod">
    <xs:restriction base="xs:string">
        <xs:enumeration value="Mastercard"/>
        <xs:enumeration value="Visa"/>
    </xs:restriction>
</xs:simpleType>
<xs:element name="PaymentMethod" nillable="true" type="tns:PaymentMethod"/>
<xs:simpleType name="RequestType">
    <xs:restriction base="xs:string">
        <xs:enumeration value="New">
            <xs:annotation>
                <xs:appinfo>
                    <EnumerationValue>1</EnumerationValue>
                </xs:appinfo>
            </xs:annotation>
        </xs:enumeration>
        <xs:enumeration value="Reload">
            <xs:annotation>
                <xs:appinfo>
                    <EnumerationValue>2</EnumerationValue>
                </xs:appinfo>
            </xs:annotation>
        </xs:enumeration>
    </xs:restriction>
</xs:simpleType>
<xs:element name="RequestType" nillable="true" type="tns:RequestType"/>

【问题讨论】:

  • 如果从枚举中去掉 DataContact 属性会发生什么?我通常会这样做以获得更好的 WSDL。
  • 感谢您的评论。它根本不影响 WSDL。

标签: .net web-services wcf wsdl


【解决方案1】:

要包含在数据协定中,您必须应用 EnumMemberAttribute 属性。在 .NET Framework 中,您始终可以将特殊值 0(零)应用于枚举,这也是任何枚举的默认值。 但是,即使这个特殊的零值也不能被序列化,除非它用 EnumMemberAttribute 属性进行标记。

这有两个例外:

  • Flag enumerations
  • EmitDefaultValue 属性设置为 false 的枚举数据成员(在这种情况下,序列化数据中会省略值为 0 的枚举)。

    [DataContract(Name = "PaymentMethod")]
    [Flags] // <---- Try this
    public enum PaymentMethod
    {   
       [EnumMember]
       Mastercard = 0,
       [EnumMember]
       Visa = 1
    }
    
    [DataContract(Name = "RequestType")]
    public enum RequestType
    {
      [EnumMember]
      New = 1,
      [EnumMember]
      Reload = 2
    }
    

【讨论】:

  • 感谢您的建议。当我将属性调整为:[DataContract(Name = "PaymentMethod")] public enum PaymentMethod { [EnumMemberAttribute(Value = "0")] Mastercard = 0, [EnumMemberAttribute(Value = "1")] Visa = 1 } 它只需将 WSDL 更改为:&lt;xs:simpleType name="PaymentMethod"&gt; &lt;xs:restriction base="xs:string"&gt; &lt;xs:enumeration value="0"/&gt; &lt;xs:enumeration value="1"/&gt; &lt;/xs:restriction&gt; &lt;/xs:simpleType&gt;
  • 加个flag怎么样?
  • 添加标志解决了!感谢您将代码添加到您的答案中。
  • 没问题!很高兴为您提供帮助! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-08
  • 1970-01-01
  • 2023-04-03
相关资源
最近更新 更多