WebServices:WSDL的結構分析
WSDL(Web Services Description Language,Web服務描述語言)是爲描述Web Services發佈的XML格式。W3C組織沒有批准1.1版的WSDL,但是2.0版本已經在製訂中,2.0版將被作爲推薦標準(recommendation)(一種官方標準),並將被W3C組織批准爲正式標準。WSDL描述Web服務的公共接口。這是一個基於XML的關於如何與Web服務通訊和使用的服務描述;也就是描述與目錄中列出的Web服務進行交互時需要綁定的協議和信息格式。通常採用抽象語言描述該服務支持的操作和信息,使用的時候再將實際的網絡協議和信息格式綁定給該服務。
WSDL 文檔僅僅是一個簡單的 XML 文檔。它包含一系列描述某個 web service 的定義。
WebMthod的定義:
1: [WebService(Namespace = "http://tempuri.org/")]
2: [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
3: [System.ComponentModel.ToolboxItem(false)]
4: public class WebService2 : System.Web.Services.WebService
5: {
6: [WebMethod]
7: public bool Add(TestClass testClass,int id)
8: {
9: return true;
10: }
11: }
12:
13: public class TestClass
14: {
15: public int a;
16: public string b;
17: public DateTime c;
18: }
19:
WSDL的結構:
一個WSDL文檔通常包含有以下元素,即types、message、portType、operation、binding、 service元素。這些元素嵌套在definitions元素中。
definitions是WSDL文檔的根元素,definitions還聲明各命名空間。
types,數據類型定義的容器,它使用某種類型系統(一般地使用XML Schema中的類型系統)。
1: <wsdl:types>
2: <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
3: <s:element name="Add">
4: <s:complexType>
5: <s:sequence>
6: <s:element minOccurs="0" maxOccurs="1" name="testClass" type="tns:TestClass" />
7: <s:element minOccurs="1" maxOccurs="1" name="id" type="s:int" />
8: </s:sequence>
9: </s:complexType>
10: </s:element>
11: <s:complexType name="TestClass">
12: <s:sequence>
13: <s:element minOccurs="1" maxOccurs="1" name="a" type="s:int" />
14: <s:element minOccurs="0" maxOccurs="1" name="b" type="s:string" />
15: <s:element minOccurs="1" maxOccurs="1" name="c" type="s:dateTime" />
16: </s:sequence>
17: </s:complexType>
18: <s:element name="AddResponse">
19: <s:complexType>
20: <s:sequence>
21: <s:element minOccurs="1" maxOccurs="1" name="AddResult" type="s:boolean" />
22: </s:sequence>
23: </s:complexType>
24: </s:element>
25: </s:schema>
26: </wsdl:types>
types描述WebMethod的名稱(Add),傳入參數(testClass——包括對TestClass的詳細描述,id),響應信息(AddResponse)。
message描述通信消息的數據結構的抽象類型化定義,使用types的描述的類型來定義整個消息的數據結構。
1: <wsdl:message name="AddSoapIn">
2: <wsdl:part name="parameters" element="tns:Add" />
3: </wsdl:message>
4: <wsdl:message name="AddSoapOut">
5: <wsdl:part name="parameters" element="tns:AddResponse" />
6: </wsdl:message>
portType和operation描述服務和服務的方法。operation包括輸入和輸出(使用message的描述)。
1: <wsdl:portType name="WebService2Soap">
2: <wsdl:operation name="Add">
3: <wsdl:input message="tns:AddSoapIn" />
4: <wsdl:output message="tns:AddSoapOut" />
5: </wsdl:operation>
6: </wsdl:portType>
binding描述Web Services的通信協議。 <soap:binding/>描述使用SOAP協議,binding還描述Web Services的方法、輸入、輸出。
1: <wsdl:binding name="WebService2Soap" type="tns:WebService2Soap">
2: <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
3: <wsdl:operation name="Add">
4: <soap:operation soapAction="http://tempuri.org/Add" style="document" />
5: <wsdl:input>
6: <soap:body use="literal" />
7: </wsdl:input>
8: <wsdl:output>
9: <soap:body use="literal" />
10: </wsdl:output>
11: </wsdl:operation>
12: </wsdl:binding>
service描述Web Services訪問點的集合。因爲包括SOAP1.1和SOAP1.2的描述,所以一個方法有對應兩描述。
1: <wsdl:service name="WebService2">
2: <wsdl:port name="WebService2Soap" binding="tns:WebService2Soap">
3: <soap:address location="http://localhost:1552/WebService2.asmx" />
4: </wsdl:port>
5: <wsdl:port name="WebService2Soap12" binding="tns:WebService2Soap12">
6: <soap12:address location="http://localhost:1552/WebService2.asmx" />
7: </wsdl:port>
8: </wsdl:service>