SOAP 通信中的数组错误解释

本文关键字:错误 解释 数组 通信 SOAP | 更新日期: 2023-09-27 18:35:17

我有一个 Java Web 服务器,我需要使用 wsdl 合约与之通信。我没有构建服务器,也无法访问它的源代码。我构建了一个 c# 应用程序,并使用 Visual Studio "add service reference" 将服务引用添加到 wsdl 协定中。我粘贴了我感兴趣的 wsdl 部分:

<wsdl:operation name="SOAPRequestItemHead" parameterOrder="SessionID searchitems">
  <wsdl:input message="impl:SOAPRequestItemHeadRequest" name="SOAPRequestItemHeadRequest"/>
  <wsdl:output message="impl:SOAPRequestItemHeadResponse" name="SOAPRequestItemHeadResponse"/>
</wsdl:operation>
<wsdl:operation name="SOAPRequestItemHead">
  <wsdlsoap:operation soapAction=""/>
  <wsdl:input name="SOAPRequestItemHeadRequest">
    <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://wrapper.soap.aplusb.com" use="encoded"/>
  </wsdl:input>
  <wsdl:output name="SOAPRequestItemHeadResponse">
    <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://192.168.200.26:8888/tcdnc/services/fsw" use="encoded"/>
  </wsdl:output>
</wsdl:operation>
<wsdl:message name="SOAPRequestItemHeadResponse">
  <wsdl:part name="SOAPRequestItemHeadReturn" type="tns2:SOAPItemRevisionHeadResult"/>
</wsdl:message>
<complexType name="SOAPItemRevisionHeadResult">
  <sequence>
    <element maxOccurs="1" minOccurs="0" name="comment" nillable="true" type="xsd:string"/>
    <element name="searchComplete" type="xsd:boolean"/>
    <element maxOccurs="unbounded" minOccurs="0" name="search" type="tns2:StringMap"/>
    <element maxOccurs="unbounded" minOccurs="0" name="resultList" type="tns2:SOAPItemRevisionHead"/>
  </sequence>
</complexType>

请注意,resultListsearch 是数组。如果我调用此方法,下面是使用 SOAP 工具获取的原始响应:

<SOAPRequestItemHeadReturn xmlns:ns2="fsw" xsi:type="ns2:SOAPItemRevisionHeadResult">
  <comment xsi:type="xsd:string" xsi:nil="true"/>
  <searchComplete xsi:type="xsd:boolean">true</searchComplete>
  <resultList xsi:type="ns2:SOAPItemRevisionHead">
    <search xsi:type="ns2:StringMap">
      <stringKey xsi:type="xsd:string">ItemRevision.ItemID</stringKey>
      <stringValue xsi:type="xsd:string">cam_english_template</stringValue>
    </search>
    <search xsi:type="ns2:StringMap">
      <stringKey xsi:type="xsd:string">ItemRevision.Revision</stringKey>
      <stringValue xsi:type="xsd:string">A</stringValue>
    </search>
    <dummy xsi:type="xsd:string" xsi:nil="true"/>
  </resultList>
  <resultList xsi:type="ns2:SOAPItemRevisionHead">
...

如您所见,resultListsearch实际上是数组。但是当我从 c# 客户端调用该方法时,出现此错误:

反序列化操作"SOAPRequestItemHead"的回复消息正文时出错。

内部异常:XML 文档 (1, 815) 中存在错误。

内部异常:无法将 StringMap 类型的对象分配给 StringMap[] 类型的对象

如果我转到自动生成的Reference.cs,并且我手动将应该是数组的两个属性的类型从 StringMap[] 更改为 StringMap则不会抛出错误,但当然我只能在我的程序中获取数组的第一项。我希望我说得很清楚,即使这是一个很长的问题。

更新:我知道这是使用 Axis 1.4 的问题,它使用 rcp/编码而不是文档/文字,因此可以用这些术语重新表述问题:".NET 可以正确处理 rcp/编码吗?

SOAP 通信中的数组错误解释

检查 VS 生成的数据协定。它们也应该包含"resultList"的特定类型,并用CollectionDataContract属性进行装饰。我还会检查命名空间和名称是否在DataContract属性上精确设置。

编辑:关于RPC风格的Web服务,您可以在这些链接中找到一些解决方法:http://social.msdn.microsoft.com/Forums/vstudio/en-US/51babae5-26e5-4405-b03c-4301710854c0/why-does-add-service-reference-fail-to-build-a-proper-reference-for-dfa?forum=wcfhttp://social.msdn.microsoft.com/Forums/vstudio/en-US/07edda1a-d0d5-4920-b2fb-a25c803269d6/trouble-with-consuming-a-java-rpc-web-service-using-net-client?forum=wcf

据我所知,WSDL 中的SOAPItemRevisionHeadResult类型定义有问题。

<complexType name="SOAPItemRevisionHeadResult">
  <sequence>
    <element maxOccurs="1" minOccurs="0" name="comment" nillable="true" type="xsd:string"/>
    <element name="searchComplete" type="xsd:boolean"/>
    <element maxOccurs="unbounded" minOccurs="0" name="search" type="tns2:StringMap"/>
    <element maxOccurs="unbounded" minOccurs="0" name="resultList" type="tns2:SOAPItemRevisionHead"/>
  </sequence>
</complexType>

具有此定义的类型映射到:

Public class SOAPItemRevisionHeadResult{
    public string comment;
    public boolean searchComplete;
    public Stringmap[] search;  
    public SOAPItemRevisionHead[] resultList;
}
Public class StringMap{
//can't see definition of this type in your part of WSDL posted but doesn't matter.
//guesed definition by looking at the response
    public string StringKey;
    public string StrigValue;
//end of guesed definition
}
Public class SOAPItemRevisionHead{
  //can't see definition of this type in your part of WSDL posted but doesn't matter.
  //guesed definition by looking at the response
    public StringMap[] search;
  //end of guesed definition
}

WSDL嵌套类似乎很糟糕,但在您发布Raw Response中,我可以看到正确的嵌套:

Public class SOAPItemRevisionHeadResult{
    public string comment;
    public boolean searchComplete;
    public resultList as SOAPItemRevisionHead[]; 
}
Public class SOAPItemRevisionHead{
    public StringMap[] search;
}
Public class StringMap{
    public string StringKey;
    public string StrigValue;
}

如果可能的话,您可能希望考虑该编码的替代方法,请参阅:
http://security-world.blogspot.dk/2005/07/nt-aspnet-rcpencoded-web-service-dos.html

我相信你正在寻找的答案就在这篇文章中:
http://social.msdn.microsoft.com/Forums/vstudio/en-US/51babae5-26e5-4405-b03c-4301710854c0/why-does-add-service-reference-fail-to-build-a-proper-reference-for-dfa?forum=wcf

上面的更正链接,不知道为什么错误的进入那里