我正在尝试编写一个使用 web 服务和 DTO 检索客户信息的 C# windows 应用程序。
我必须使用提供的使用 DTO(数据传输对象)的网络服务
在 Soap UI 中,请求 XML 看起来像
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:open="http://www.openuri.org/">
<soapenv:Header/>
<soapenv:Body>
<open:getCustomer>
<open:customerNumber>123456</open:customerNumber>
</open:getCustomer>
</soapenv:Body>
</soapenv:Envelope>
DTO 返回是
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header/>
<env:Body>
<getCustomerResponse xmlns="http://www.openuri.org/" xmlns:ns2="http://www.openuri.org">
<getCustomerResult>
<Name>Customer Name</Name>
<Address1>Address 1</Address1>
<Address2>Address 2</Address2>
<City>City</City>
<State>State</State>
<Zip>zip</Zip>
</getCustomerResult>
</getCustomerResponse>
</env:Body>
</env:Envelope>
现在,在 Visual Studio 中添加 Web 引用后,我可以调用实例化 Web 服务并使用以下内容传递成员 ID。
public void getCompanyGreeting(int inCustomerNumber)
{
WebReference.getCustomer getCustomerInfo = new WebReference.getCustomer();
getCustomerInfo.customerNumber = inCustomerNumber;
}
问题是如何获取 DTO 返回的数据,以便将其写入表单上的适当文本字段?
使用 Visual Studio 时,我希望看到类似 getCustomerInfo.Address1 的内容,但由于数据位于 DTO 中,因此我无法使用此方法看到返回。如果我实例化 getCustomerResponse DTO,我确实会看到 getCustomerResponse.Address1 但我无法将值 inCustomerNumber 传递给它,因为它不接受输入参数。
Dataflow = 使用 customerNumber 调用 getCustomer 并返回 getCustomerResponse DTO 中的所有值。
我的服务代码如下
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.openuri.org/")]
public partial class getCustomer {
private int customerNumberField;
/// <remarks/>
public int customerNumber {
get {
return this.customerNumberField;
}
set {
this.customerNumberField = value;
}
}
}
对于它创建的 DTO
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.openuri.org/")]
public partial class AccountDTO {
private string nameField;
private string address1Field;
private string address2Field;
private int cityField;
private string stateField;
private string zipField;
/// <remarks/>
public string Name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
/// <remarks/>
public string Address1 {
get {
return this.address1Field;
}
set {
this.address1Field = value;
}
}
/// <remarks/>
public string Address2 {
get {
return this.address2Field;
}
set {
this.address2Field = value;
}
}
/// <remarks/>
public int City {
get {
return this.cityField;
}
set {
this.cityField = value;
}
}
/// <remarks/>
public string State {
get {
return this.stateField;
}
set {
this.stateField = value;
}
}
/// <remarks/>
public string Zip {
get {
return this.zipField;
}
set {
this.zipField = value;
}
}
}
我必须使用的 WSDL
<definitions name='AccountService' targetNamespace='http://www.openuri.org/' xmlns='http://schemas.xmlsoap.org/wsdl/' xmlns:ns1='http://www.openuri.org' xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' xmlns:tns='http://www.openuri.org/' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<types>
<xs:schema targetNamespace='http://www.openuri.org' version='1.0' xmlns:ns1='http://www.openuri.org/' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:import namespace='http://www.openuri.org/'/>
<xs:element name='getCustomer'>
<xs:complexType>
<xs:sequence>
<xs:element ref='ns1:customerNumber'/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<xs:schema targetNamespace='http://www.openuri.org/' version='1.0' xmlns:tns='http://www.openuri.org/' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:element name='customerNumber' type='xs:int'/>
<xs:element name='getCustomer' nillable='true'>
<xs:complexType>
<xs:sequence>
<xs:element form='qualified' name='customerNumber' type='xs:int'/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name='getCustomerResponse'>
<xs:complexType>
<xs:sequence>
<xs:element form='qualified' minOccurs='0' name='getCustomerResult' type='tns:AccountDTO'/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name='AccountDTO'>
<xs:sequence>
<xs:element form='qualified' minOccurs='0' name='Name' type='xs:string'/>
<xs:element form='qualified' minOccurs='0' name='Address1' type='xs:string'/>
<xs:element form='qualified' minOccurs='0' name='Address2' type='xs:string'/>
<xs:element form='qualified' name='City' type='xs:string'/>
<xs:element form='qualified' minOccurs='0' name='State' type='xs:string'/>
<xs:element form='qualified' minOccurs='0' name='Zip' type='xs:string'/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</types>
<message name='AccountService_getCustomerResponse'>
<part element='tns:getCustomerResponse' name='getCustomerResponse'></part>
</message>
<message name='AccountService_getCustomer'>
<part element='tns:getCustomer' name='getCustomer'></part>
</message>
<portType name='AccountService'>
<operation name='getCustomer' parameterOrder='getCustomer'>
<input message='tns:AccountService_getCustomer'></input>
<output message='tns:AccountService_getCustomerResponse'></output>
</operation>
</portType>
<binding name='AccountServiceBinding' type='tns:AccountService'>
<soap:binding style='document' transport='http://schemas.xmlsoap.org/soap/http'/>
<operation name='getCustomer'>
<soap:operation soapAction='http://www.openuri.org/getCustomer'/>
<input>
<soap:body use='literal'/>
</input>
<output>
<soap:body use='literal'/>
</output>
</operation>
</binding>
<service name='AccountService'>
<port binding='tns:AccountServiceBinding' name='AccountServiceSoap'>
<soap:address location='http://xxx.xxx.xxx.xxx:8080/account/AccountService'/>
</port>
</service>
</definitions>