Binding to a SOAP Service With Optional Value Types

落爺英雄遲暮 提交于 2020-01-03 02:17:05

问题


I have a method on a SOAP service being generated with the following 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>

I'm connecting to this service and generating the client using Visual Studio in a .NET Framework 4.7 desktop application.

This generates a method with the following parameters:

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

According to the service definition, isActive and isPrimary are optional parameters, but in the generated method they are non-nullable value types.

Is there a way to generate the client to allow these to be optional, perhaps via nullable booleans?


回答1:


I did finally find the answer to this question, but it does feel like the default behavior is a bug. In the generated Reference.svcmap document, you can add a <Wrapped>true</Wrapped> to force the *Specified fields to show up. You must add Wrapped under the ClientOptions node, like so:

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

Regenerating the client now will force use of message contract objects, so the call will now look like this:

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

I'm relieved to have the solution, but still think this should be the default way of generating message contracts to allow for this scenario.



来源:https://stackoverflow.com/questions/45743085/binding-to-a-soap-service-with-optional-value-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!