繁体   English   中英

使用Web服务中的数据传输对象

[英]Using Data Transfer Objects from Web Service

我正在尝试编写一个使用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>

您说过, If I instantiate the getCustomerResponse DTO I do see getCustomerResponse.Address1 but I can not pass the value inCustomerNumber to it because it does not accept input parameters. -您不能将任何参数作为类而不是方法传递给它。

您可以按照以下方式调用帐户服务并获得所需的输出:

WebReference.getCustomer cust = new WebReference.getCustomer();
cust.customerNumber = 123456; //pass customer number
WebReference.AccountService acService = new WebReference.AccountService();
WebReference.getCustomerResponse acServiceResponce = acService.getCustomer(cust); //pass object of getCustomer class to getCustomer method  
string address1 = acServiceResponce.getCustomerResult.Address1;
string city = acServiceResponce.getCustomerResult.City;

希望这可以帮助您从此Web服务获得响应

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM