Web サービスと DTO を使用して顧客情報を取得する C# Windows アプリケーションを作成しようとしています。
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>