【发布时间】:2021-03-06 02:38:41
【问题描述】:
我们正在尝试为 PHP SOAP 服务实现 WCF 代理/客户端。 我们检索了 WSDL 并在命令行应用程序中创建了一个服务引用。 我们能够检索 SOAP 操作的结果,但事实证明,返回的列表总是以 null 结尾,当使用 fiddler 敲击线路或在 SoapUI 中对其进行测试时,我们可以清楚地看到正在返回的数据。
我在类似的问题帖子中遵循了一些建议,并提出了一个似乎存在相同问题的简约反序列化示例。
Message m = Message.CreateMessage(XmlReader.Create("response.xml"), int.MaxValue, MessageVersion.Soap11);
SoapReflectionImporter importer = new SoapReflectionImporter(new SoapAttributeOverrides(), "http://brokenns");
XmlTypeMapping mapping = importer.ImportTypeMapping(typeof(getDeviceListResponse));
XmlSerializer serializer = new XmlSerializer(mapping);
getDeviceListResponse response = (getDeviceListResponse)serializer.Deserialize(m.GetReaderAtBodyContents());
Console.WriteLine(response.@return.deviceList == null ? "List is null" : "List is not null");
< List is null
service.wsdl
<?xml version="1.0" encoding="utf-8"?>
<definitions xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:tns="http://brokenns" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/"
targetNamespace="http://brokenns">
<types>
<xsd:schema targetNamespace="http://brokenns">
<xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
<xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" />
<xsd:complexType name="Device">
<xsd:sequence>
<xsd:element name="id" type="xsd:int" minOccurs="1" maxOccurs="1" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="DeviceListResult">
<xsd:sequence>
<xsd:element name="message" type="xsd:string" minOccurs="0" maxOccurs="1" />
<xsd:element name="deviceList" type="tns:Device" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</types>
<message name="getDeviceListRequest">
<part name="loadSimCard" type="xsd:boolean" />
</message>
<message name="getDeviceListResponse">
<part name="return" type="tns:DeviceListResult" />
</message>
<portType name="DevicesServicePortType">
<operation name="getDeviceList">
<documentation>Returns device list</documentation>
<input message="tns:getDeviceListRequest" />
<output message="tns:getDeviceListResponse" />
</operation>
</portType>
<binding name="DevicesServiceBinding" type="tns:DevicesServicePortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
<operation name="getDeviceList">
<soap:operation soapAction="https://myservice.com/ws/devices.php/getDeviceList" style="rpc" />
<input>
<soap:body use="literal" namespace="http://brokenns" /></input>
<output>
<soap:body use="literal" namespace="http://brokenns" /></output>
</operation>
</binding>
<service name="DevicesService">
<port name="DevicesServicePort" binding="tns:DevicesServiceBinding">
<soap:address location="https://myservice.com/ws/devices.php" />
</port>
</service>
</definitions>
response.xml
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:getDeviceListResponse xmlns:ns1="http://brokenns">
<return>
<message>works</message>
<deviceList>
<id>48</id>
</deviceList>
<deviceList>
<id>41</id>
</deviceList>
<deviceList>
<id>42</id>
</deviceList>
</return>
</ns1:getDeviceListResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
任何人都可以发现服务 wsdl 中的错误会在反序列化后使我的列表为空吗?
我尝试对生成的合约进行简单的来回序列化,但无法在其中发现问题,这让我相信它与命名空间有关,但省略它们并没有帮助。
<?xml version="1.0" encoding="utf-16"?>
<getDeviceListResponse xmlns:ns1="http://brokenns">
<return>
<message>sd</message>
<deviceList>
<id>2323</id>
</deviceList>
<deviceList>
<id>67</id>
</deviceList>
</return>
</getDeviceListResponse>
【问题讨论】:
-
原因可能是 WCF 将通用列表序列化为数组以通过网络发送。该配置只是告诉 svcutil 创建一个代理并将它们转换回一个通用列表以方便使用。您可以在创建服务引用时尝试修改集合类型。更多相关信息可以参考这个链接:stackoverflow.com/questions/63702018/…
-
在 wcf 代理中反序列化后,任何集合类型都不会改变列表为空的事实。
-
您想在.net 中调用PHP SOAP 服务吗?如果你想在.net中调用PHP SOAP服务可以参考这个链接:forums.asp.net/t/…
-
该服务确实在另一端使用 NuSoap,但该链接对我没有答案。
标签: c# visual-studio wcf soap wsdl