我有一个涉及C#控制台客户端和Web服务的有趣问题。 我的怀疑是某个地方有一个名称空间错误或一些简单的事情,但八个小时茫然地盯着屏幕尚未透露它们,所以我想我会再试一眼。
WSDL
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://
schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:schema="http://xxx.com/BillOfMaterials/schemas" xmlns:tns="http://
xxx.com/BillOfMaterials/definitions"
targetNamespace="http://xxx.com/BillOfMaterials/definitions">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import namespace="http://xxx.com/BillOfMaterials/
schemas" schemaLocation="BOMRequest.xsd" id="input"/>
<xs:import namespace="http://xxx.com/BillOfMaterials/schemas"
schemaLocation="BOMResponse.xsd" id="output"/>
</xs:schema>
</wsdl:types>
<wsdl:message name="BomRequest">
<wsdl:part name="input" element="schema:BOMin"/>
</wsdl:message>
<wsdl:message name="BomResponse">
<wsdl:part name="output" element="schema:BOMResponse"/>
</wsdl:message>
<wsdl:portType name="BOMPortType">
<wsdl:operation name="BOM">
<wsdl:input message="tns:BomRequest"/>
<wsdl:output message="tns:BomResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="BOMBinding" type="tns:BOMPortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/
http"/>
<wsdl:operation name="BOM">
<soap:operation soapAction="urn:#BOMOperation"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="BOMService">
<wsdl:port name="BOMPort" binding="tns:BOMBinding">
<soap:address location="http://xxx/BOM"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
申请XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/
XMLSchema" xmlns:in="http://secotools.com/BillOfMaterials/schemas"
targetNamespace="http://xxx.com/BillOfMaterials/schemas" elementFormDefault="qualified">
<xs:element name="BOMin" type="in:BOMRequestItem"/>
<xs:complexType name="BOMRequestItem">
<xs:sequence>
<xs:element name="ProductNumber" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
响应XSD(缩短,因为它很大)
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/
XMLSchema" xmlns:in="http://xxx.com/BillOfMaterials/schemas"
targetNamespace="http://xxx.com/BillOfMaterials/schemas"
elementFormDefault="qualified">
<xs:element name="BOMResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="BOMResponses" type="in:BOMResponseItem"
minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="BOMResponseItem">
<xs:sequence>
<xs:element name="Company" type="xs:string"/>
<xs:element name="Facility" type="xs:string"/>
<xs:element name="ProductNumber" type="xs:string"/>
<xs:element name="ProductStructureType" type="xs:string"/>
<xs:element name="Name" type="xs:string"/>
使用此WSDL,我在soapUI中获得以下请求和响应: SoapUI请求:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:sch="http://xxx.com/BillOfMaterials/schemas">
<soapenv:Header/>
<soapenv:Body>
<sch:BOMin>
<ProductNumber>12345</ProductNumber>
</sch:BOMin>
</soapenv:Body>
</soapenv:Envelope>
SoapUI响应(为了便于阅读而缩短):
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:out="http://xxx.com/BillOfMaterials/schemas">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<out:BOMResponse>
<Company>1</Company>
<Facility>1</Facility>
<ProductNumber>12345</ProductNumber>
<ProductStructureType>PLU</ProductStructureType>
<Name>My dummy product</Name>
我已尝试使用svcutil生成代理并在C#中使用服务引用无效。 SoapUI按预期工作并返回数据,而C#获取一个空列表。
C#测试代码
class Program
{
static void Main(string[] args)
{
BOMPortTypeClient _client = new BOMPortTypeClient();
var state = _client.State;
BOMRequestItem _reqItem = new BOMRequestItem
{
ProductNumber = "12345"
};
IList<BOMResponseItem> _list = _client.BOM(_reqItem);
}
}
编辑:添加来自.Net
的请求/响应的Fiddler日志.Net请求
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<BOMin xmlns="http://xxx.com/BillOfMaterials/schemas">
<ProductNumber>12345</ProductNumber>
</BOMin>
</s:Body>
</s:Envelope>
.Net回复
<?xml version="1.0" ?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://xxx.com/BillOfMaterials/schemas" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<s:Header/>
<s:Body>
<BOMResponse>
<Company>1</Company>
<Facility>1</Facility>
<ProductNumber>12345</ProductNumber>
<ProductStructureType>PLU</ProductStructureType>
<Name>My dummy product</Name>
由于我确实收到数据,我知道服务理解来自.Net的请求,问题是.Net似乎无法理解响应。这就是为什么我首先想到的是命名空间问题。现在我不再那么肯定了。我在.Net中没有错误,只是一个空数组。