绑定到具有可选值类型的 SOAP 服务

Binding to a SOAP Service With Optional Value Types

我在 SOAP 服务上有一个方法,使用以下 WSDL:

<xs:complexType name="updateItem">
  <xs:sequence>
    <xs:element name="itemCode" type="xs:string" />
    <xs:element minOccurs="0" name="itemParentCode" type="xs:string" />
    <xs:element minOccurs="0" name="itemStatus" type="xs:string" />
    <xs:element minOccurs="0" name="isActive" type="xs:boolean" />
    <xs:element minOccurs="0" name="isPrimary" type="xs:boolean" />
  </xs:sequence>
</xs:complexType>

我正在连接到此服务并在 .NET Framework 4.7 桌面应用程序中使用 Visual Studio 生成客户端。

这将生成一个具有以下参数的方法:

public void updateItem(string itemCode, string itemParentCode, 
    string itemStatus, bool isActive, bool isPrimary)

根据服务定义,isActiveisPrimary是可选参数,但在生成的方法中它们是不可为空的值类型。

有没有办法生成客户端以允许这些是可选的,也许通过可为空的布尔值?

我终于找到了这个问题的答案,但感觉默认行为是一个错误。在生成的 Reference.svcmap 文档中,您可以添加 <Wrapped>true</Wrapped> 以强制显示 *Specified 字段。您必须在 ClientOptions 节点下添加 Wrapped,如下所示:

<ClientOptions>
    <Wrapped>true</Wrapped>
</ClientOptions>

现在重新生成客户端将强制使用消息协定对象,因此调用现在如下所示:

// Update an item
updateItem(new updateItem 
    { 
        itemCode = "testItem", 
        itemParentCode = "testParent", 
        itemStatus = "testStatus", 
        isActive = true, 
        isPrimary = true, 
        isActiveSpecified = true, 
        isPrimarySpecified = true 
    });

我很高兴有解决方案,但仍然认为这应该是生成消息合同的默认方式以允许这种情况。