使用Web Service中的数据传输对象

时间:2014-12-30 17:45:34

标签: c# .net web-services windows-applications data-transfer-objects

我正在尝试编写一个C#windows应用程序,该应用程序使用Web服务和DTO检索客户信息。

我必须使用提供的使用DTO(数据传输对象)的Web服务

在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中添加webreference后,我可以调用实例化webservice并使用以下内容传递成员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>

1 个答案:

答案 0 :(得分:1)

你说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服务获得响应