避免在运行时序列化SOAP属性

我们使用了一个具有SaveCustomer功能的SOAP服务。如果在字段中指定了值,则SaveCustomer函数将使用下面列出的Customer对象,并更新更改。

今天我们发现,如果我们只想更新性别字段,就不应该发送其他任何字段,甚至不发送为null。基本上,我们必须避免序列化。 问题是,如果我们发送以下内容:

<Customer>
       <customerIdField>1</customerIdField>
       </emailField>
       <genderField>5</genderField>
</Customer>

然后系统将删除电子邮件字段。我们如何在运行时轻松地指定对象的字段是否应包括在序列化中?我看过类似SOAPignore的东西,但是看起来非常脆弱并且使用起来很危险。例如,如果将新字段添加到对象怎么办?

同时,我们实际上必须能够将一个字段指定为null,以防实际需要从我们使用的系统中的客户中删除该字段。

[System.CodeDom.Compiler.GeneratedCodeAttribute("system.xml","4.8.3752.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepthroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[system.xml.Serialization.XmlTypeAttribute(Namespace="http://www.namespace.come/")]
public partial class Customer : CompanyEntity {

    private string customerIdField;

    private string firstNameField;

    private string lastNameField;

    private Address[] addressField;

    private PhoneNumber[] phoneNumberListField;

    private string emailField;

    private EmailBounce emailBounceField;

    private Gender genderField;

    private string ssnField;

    private bool controlStatusField;

    private CustomerProperties propertiesField;

    private CreditCheck creditCheckField;

    private ClubProperties clubPropertiesField;

    private CustomerConsent[] customerConsentField;

    private SalesChannelInfo[] salesChannelInfoListField;
lizhc 回答:避免在运行时序列化SOAP属性

考虑到您的项目与我的项目之间的差异,您缺少数据合约类中的“指定”字段。

给出wsdl:

        <xs:complexType name="setAuthorization">
            <xs:sequence>
                <xs:element form="qualified" minOccurs="0" name="action" type="tns:actionType"/>
                <xs:element form="qualified" minOccurs="0" name="role" type="tns:roleType" />
            </xs:sequence>
        </xs:complexType>

您应该生成诸如;

之类的字段
[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil","4.7.3081.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:si-dmp-habilitations")]
public partial class setAuthorization
{

    private actionType actionField;

    private bool actionFieldSpecified;

    private roleType roleField;

    private bool roleFieldSpecified;

这是“指定的”字段,可让您区分现有的空删除值和不更改此值的空Im值。

检查您的WSDL是否包含minOccurs =“ 0”。并查看生成代理的方式。在我的示例中,我使用具有以下语法的SvcUtil:

svcutil  /t:code /l:c# /o:"C:\Temp\GeneratedServiceReferences\HabilitationsService.cs" /config:"C:\Temp\GeneratedServiceReferences\HabilitationsService.config" /namespace:*,TLSiKitEditeur.HabilitationService /serializer:Auto /wrapped      wsdl\Habilitations.wsdl

我希望对您有帮助

本文链接:https://www.f2er.com/2979401.html

大家都在问