在WSDL中使用“”设置minOccurs=0;指定的“;模式不起作用

本文关键字:不起作用 模式 minOccurs WSDL 设置 | 更新日期: 2023-09-27 17:58:18

好吧,显然我做错了什么。我正在尝试创建一个网络服务,我想要";dateShipped";是可选的,这意味着在WSDL中,我希望minOccurs="0";

[Serializable]
[XmlType]
public class CTShipment
{
    [XmlElement(Order = 0, IsNullable=false)] public CTDeliveryMethod DeliveryMethod;
    [XmlElement(Order = 1, IsNullable=false)] public CTShipmentAddress ShipmentAddress;
    [XmlIgnore] public bool dateShippedSpecified;
    [XmlElement(Order = 2, IsNullable=false)] public DateTime dateShipped;
}

我希望WSDL像这样生成:

<xs:complexType name="CTShipment">
  <xs:annotation>
     <xs:documentation>All details for the shipment of a suborder.</xs:documentation>
  </xs:annotation>
  <xs:sequence>
     <xs:element name="DeliveryMethod" type="CTDeliveryMethod" nillable="false"/>
     <xs:element name="ShipmentAddress" type="CTShipmentAddress" nillable="false"/>
     <xs:element name="dateShipped" type="xs:dateTime" nillable="false" minOccurs="0"/>
  </xs:sequence>
</xs:complexType>

相反,我实际得到的是:

<xs:complexType name="CTShipment">
  <xs:sequence>
     <xs:element name="DeliveryMethod" nillable="true" type="tns:CTDeliveryMethod"/>
     <xs:element name="ShipmentAddress" nillable="true" type="tns:CTShipmentAddress"/>
     <xs:element name="dateShipped" type="xs:dateTime"/>
     <xs:element name="dateShippedSpecified" type="xs:boolean"/>
  </xs:sequence>
</xs:complexType>

根据我读过的几件事(包括http://msdn.microsoft.com/en-us/library/zds0b35c%28v=vs.90%29.aspx)包括公众的嘘声";指定日期发货";应使";dateShipped";可选(minOccurs=0)。正如你所看到的,这不仅没有发生,而且";指定日期发货";显示在WSDL中,即使它被标记为";[XmlIgnore]";。您可能已经注意到还有另一个问题:即使我指定了";IsNullable=false";,我仍然得到nillable=";真";在WSDL中。

这不少于4个我无法解释的问题都与同一件事有关:

  1. 如何在WSDL中将minOccurs设置为0?

  2. 为什么指定的[fieldName]模式不使[fieldName]可选(minOccurs=0)?

  3. 即使它没有遵循___指定的模式,如果用XmlIgnore标记,为什么dateShippedSpecified会显示在WSDL中?

  4. 为什么所有的东西都被标记为nillable=";真";即使我指定";IsNullable=false;?

    作为一个额外的问题,如果有人知道。。。

  5. 如何获得要包含的注释(如下所示)?

    <xs:annotation>
     <xs:documentation>All details for the shipment of a suborder.</xs:documentation>
    </xs:annotation>
    

在WSDL中使用“”设置minOccurs=0;指定的“;模式不起作用

这是由于Sequence元素造成的。它指定每个元素的minOccurs=1。WSDL使用序列元素而不是"全部",因为您为它们指定了Order。这要求每个值都存在。

因此,当你删除订单时,它应该已经准备好了。如果你真的需要订单,那么你没有办法忽略这个价值。

这是.net实现中的一个bug。

根据W3C规范(针对wsdl),minOccurs="0"可以在序列中使用。"<序列>"是指按顺序出现0次或更多次的元素。

例如,看看W3C对wsdl的官方定义:http://www.w3.org/TR/wsdl

您将看到以下元素:

<sequence>
         <element ref="wsdl:documentation" minOccurs="0"/>
 </sequence>

现在,当需要与.Net兼容时,请使用nillable="true"哪个会给你DateTime?(可为null的版本),而不是DateTime。