来自 SoapUI 的缺少 int 参数的 Web 服务请求在 minOccurs=1 时自动分配 0 值

Web service request from SoapUI with a missing int parameter automatically assigns 0 value when minOccurs=1

我有一个只有一个网络方法的网络服务:

[WebMethod]
public string SendInt([XmlElement(IsNullable=false)] int someInt)
{
    return "Hello World";
}

此网络方法生成的 WSDL 包括以下几行:

<s:element name="SendInt">
   <s:complexType>
      <s:sequence>
         <s:element minOccurs="1" maxOccurs="1" name="someInt"   type="s:int"/>
      </s:sequence>
   </s:complexType>
</s:element>

WSDL 明确指出请求参数是具有 minOccurs=1 的值类型 (int),但是当我使用 SoapUI 发送以下请求时:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:SendInt>
      </tem:SendInt>
   </soapenv:Body>
</soapenv:Envelope>

我从服务器得到了一个有效的响应,而不是一个错误,表明缺少 someInt 参数。

在服务器中,我可以看到为 someInt 参数分配了 0 值。

这是正常行为吗?

如果是这样,我如何在我的网络方法中知道用户是否发送了 0 值或只是忘记发送此参数?

如果这不是正常行为,我该如何解决?

嘿,当您在请求中指定元素时,您只是没有为其分配值。 int 的默认值是 0 所以是的,这是正常行为。如果您遗漏该元素,您将收到无效请求。如果他们忘记发送此参数,则整个元素可能不会正确显示。在那种情况下,网络服务会抛出一个错误