I am trying to write a C# windows application that retrieves customer information using a webservice and DTO.
I have to use the provided webservice that uses DTO (Data Transfer Object)
in Soap UI the request XML looks like
<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>
The DTO Return is
<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>
Now after adding the webreference in Visual Studio I can call instantiate the webservice and pass the member id with the following.
public void getCompanyGreeting(int inCustomerNumber)
{
WebReference.getCustomer getCustomerInfo = new WebReference.getCustomer();
getCustomerInfo.customerNumber = inCustomerNumber;
}
The issue is how do I get the DTO returned data, so I can write that to the appropriate text feilds on a form?
When using Visual Studio I would expect to see something like getCustomerInfo.Address1 but because the data is in DTO I can not see the return using this method. 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.
Dataflow = call the getCustomer with the customerNumber and that returns all the values in the getCustomerResponse DTO.
My Service code is as follows
[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;
}
}
}
For The DTO it created
[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;
}
}
}
The WSDL I have to use
<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>
You said that 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.
- You cannot pass any parameter to it as its a class not a method.
You can invoke account service like below way and get required output:
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;
Hope this helps you to get response from this Web Service