【问题标题】:WSDL generated code not deserializing long in XML properlyWSDL 生成的代码在 XML 中没有正确反序列化
【发布时间】:2012-04-02 19:22:34
【问题描述】:

我们的一个供应商给了我一个 WSDL,其中包含以下内容:

<xsd:element name="RegistrationResponse">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="DateTimeStamp" type="xsd:dateTime" nillable="true"/>
            <xsd:element name="EchoData" type="xsd:string" nillable="true"/>
            <xsd:element name="TransactionTrace" type="xsd:long" nillable="true"/>
            <xsd:element name="ResponseCode" type="xsd:int" nillable="true"/>
            <xsd:element name="ResponseMessage" type="xsd:string" nillable="true"/>
            <xsd:element name="ClientAccNumber" type="xsd:long" nillable="true"/>
            <xsd:element name="BranchCode" type="xsd:int" nillable="true"/>
            <xsd:element name="HIN" type="xsd:long" nillable="true"/>
            <xsd:element name="EasyPayRef" type="xsd:long" nillable="true"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

但有时我从他们那里得到的回复不会包含所有字段。例如在这种情况下:

 <soapenv:Body>
    <tpw:RegistrationResponse>
      <DateTimeStamp>
        2012-04-02T19:10:41.4430564Z
      </DateTimeStamp>
      <EchoData/>
      <TransactionTrace>
        5418721751027669946
      </TransactionTrace>
      <ResponseCode>
        25
      </ResponseCode>
      <ResponseMessage>
        Invalid Mobile Account Type
      </ResponseMessage>
      <ClientAccNumber/>
      <BranchCode/>
      <HIN>
        0
      </HIN>
      <EasyPayRef/>
    </tpw:RegistrationResponse>
  </soapenv:Body>

现在 Visual Studio 中的代码在添加服务引用时生成的代码不喜欢 ClientAccNumber 为空白的事实。生成的代码如下:

[System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://tpwebservice.x.com", Order=5)]
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
    public System.Nullable<long> ClientAccNumber;

当它尝试反序列化从服务器收到的响应时,我收到“输入格式不正确”异常。我在想的是它看到一个空白字符串并尝试从中解析出很长的字符串,这显然失败了。我尝试将 minOccurs="0" 添加到没有帮助的 wsdl。

如何修复 wsdl 或生成的代码来解决这个问题?还是我还缺少其他东西?

【问题讨论】:

    标签: c# wcf web-services soap wsdl


    【解决方案1】:

    我会更改代码以将属性定义为字符串,并拥有一个非 XML 可序列化的属性,其中包含与字符串转换为/从字符串转换的实际 Nullable&lt;long&gt; 值:

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://tpwebservice.x.com", Order=5)] 
    [System.Xml.Serialization.XmlElementAttribute("ClientAccNumber", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] 
    public string ClientAccNumberStr; 
    
    [System.Xml.Serialization.XmlIgnoreAttribute]
    public System.Nullable<long> ClientAccNumber {
      get {
        if (string.IsNullOrEmpty(ClientAccNumberStr))
          return null;
        return long.Parse(ClientAccNumberStr);
      }
      set {
        if (!value.HasValue) {
          ClientAccNumberStr = null;
        } else {
          ClientAccNumberStr = value.Value.ToString();
        }
      }
    }
    

    【讨论】:

    • 但是为了添加额外的属性,我需要编辑生成的代码,这变成了一场噩梦,因为每次重新生成 WSDL 时,更改都会丢失。
    • 您的 WSDL 与您获得的 XML 不匹配 - 您必须在每次修改时修复 WSDL 或修复代码,我看不出有什么办法.
    • 或者要求供应商发送一个nil而不是一个空元素?或者你能以某种方式预处理他们的 XML 并纠正它是必要的吗?
    • 我让他们发送一个“”,但这仍然会破坏反序列化。 Nillable 在 WSDL 中指定。为什么还是坏掉?
    • 为我工作,带有一个简单的测试类。您遇到了哪个错误?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-06
    • 2019-04-06
    相关资源
    最近更新 更多