html5中文学习网

您的位置: 首页 > 网络编程 > ASP.NET » 正文

WSDL文件详解(转贴)上_.NET教程_编程技术

[ ] 已经帮助:人解决问题
详解
作者:Carlos C. Tapang
Infotects

2001 年 7 月

摘要:只要使用 WSDL,即可以真正不受言平台限制的方式,自路服生 Proxy。(列印共 28 )


使用 WSDL 的原因
WSDL 文件
WSDL 例案
命名空
SOAP 息
WSDL 型息段中的 XML 描述
<portType> <operation> 元素
<binding> <operation> 元素
文件式
<service> <port> 元素



使用 WSDL 的原因
路通定之的,到底是某制施行的;或是,人真的,遵守的於必付出的代?所提出的,多都疾而。有候,未被大所使用的,法律或政府定是硬要使用:好比,Ada 程式言。

我相信,遵守所能得的,才是使普及的原因。例如,路服的重是,即使不同公司所建造的列道,也可以接在一起;也就是,不同公司的品必能整合使用。因此,家商便共同推出了 SOAP 。WSDL (Web Services Description Language,路服描述言) 可易路服提供商服的使用者合起,取 SOAP 的。不同公司所建造的列道比容易整合;竟,必同意的不是之的距而已。但路服而言,情得多了。首先必取得的共是,指定介面的格式。

有一直,SOAP 不需要介面描述言。若 SOAP 粹是通容的,那它需要的便是描述容的言。SOAP 息可型,也因此 SOAP 允以的方式定型。但除非知道函的名、、型,否根本法正呼叫任何函。若不使用 WSDL,是可以所提供的文件或查路息,定呼叫的法。但方式都需要人力介入,也因此可能在程中出。若使用 WSDL,即可以真正不受言平台限制的方式,自路服生 Proxy。似 CORBA 或 COM 的 IDL 案,WSDL 案也是一客端伺服端之的合。

注意,然 WSDL 的目的是, SOAP 以外的通定示;但本文的主旨是在 HTTP 上 SOAP 有的 WSDL。而且然目前 SOAP 的主要用途是端程序或函呼叫,但 WSDL 已可以在 SOAP 下,指定的文件。WSDL 1.1 已以 Note (通知) 的方式 ( http://www.w3.org/TR/wsdl.html),提交至 W3C 。

WSDL 文件
若欲解任何 XML 文件,是很有助益的。下明 WSDL 的;它是一 XML 文件,可示 WSDL 文件五成段之的。

WSDL 文件可分成段群。上群是由抽象定 (Abstract Definitions) 所成;而下群是由具定 (Concrete Descriptions) 所成。抽象段定 SOAP 息的方式是,排除平台言的限制;因此它不含任何或言特有的元素。如此一,不同的站皆可作它所定的服。如序列化等站特有的,交由含具描述的下段理。

抽象定
Types (型)
不受言限制的型定。
Messages (息)
含函 (入出分) 或文件描述。
PortTypes (埠型)
根 Messages 段中的息定,明函章 (作名、入、出)。
具定
Bindings ()
在 PortTypes 段中,指定每作的。
Services (服)
指定每的埠位址。
在下中,箭接代表,在文件的不同段之有性存在。箭接代表「照」或「使用」。箭接代表「修改 (modifier)」。3-D 箭接代表「包含 (contains)」。因此,Messages 段使用 Types 段的定;PortTypes 段使用 Messages 段的定;Bindings 段照 PortTypes 段;Services 段照 Bindings 段。PortTypes Bindings 段含作元素,而 Services 段含埠元素。Bindings 段中的作元素,修改或一步明 PortTypes 段中的作元素。

以此基,本文使用 XML 技,明 WSDL 文件。「元素」一是指 XML 元素,而「性」一是指元素性。因此:

<element attribute="attribute-value">contents</element>

容可以的方式,由一以上的元素成。根元素是最端的元素,文件中其它所有元素皆在其下。子元素永附於其它的父元素。

注意,只可以有一 Types 段,甚或根本有此段。其它所有段可以有零、一、或多父元素。例如,Messages 段可以有零、或多 <message> 元素。WSDL 描述定,所有段必依指定序排列:入、型、息、portType、、服。每抽象段可能各自位於不同的案,分入至主文件中。



[ 1] 抽象具定

WSDL 例案
在一同深入探 WSDL 例案,以其作方式。得是一非常的 WSDL 文件例。其目的在解最明的功能。下列各段皆有更的。

<?xml version="1.0" encoding="UTF-8" ?>
<definitions name="FooSample"
targetNamespace="http://tempuri.org/wsdl/"
xmlns:wsdlns="http://tempuri.org/wsdl/"
xmlns:typens="http://tempuri.org/xsd"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:stk="http://schemas.microsoft.com/soap-toolkit/wsdl-extension"
xmlns="http://schemas.xmlsoap.org/wsdl/">

<types>
<schema targetNamespace="http://tempuri.org/xsd"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
elementFormDefault="qualified" >
</schema>
</types>

<message name="Simple.foo">
<part name="arg" type="xsd:int"/>
</message>

<message name="Simple.fooResponse">
<part name="result" type="xsd:int"/>
</message>

<portType name="SimplePortType">
<operation name="foo" parameterOrder="arg" >
<input message="wsdlns:Simple.foo"/>
<output message="wsdlns:Simple.fooResponse"/>
</operation>
</portType>

<binding name="SimpleBinding" type="wsdlns:SimplePortType">
<stk:binding preferredEncoding="UTF-8" />
<soap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="foo">
<soap:operation
soapAction="http://tempuri.org/action/Simple.foo"/>
<input>
<soap:body use="encoded" namespace="http://tempuri.org/message/"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body use="encoded" namespace="http://tempuri.org/message/"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
</binding>

<service name="FOOSAMPLEService">
<port name="SimplePort" binding="wsdlns:SimpleBinding">
<soap:address location="http://carlos:8080/FooSample/FooSample.asp"/>
</port>
</service>
</definitions>

下列此例文件的大略明。稍後,就每段。

第一行宣告,本文件 XML。然非是必要,但它可助 XML 剖析器定,剖析此 WSDL 案,或出。第二行是 WSDL 文件中的根元素:<definitions>。有命名空性 (命名空宣告),附於此根元素以及 <types> 元素的 <schema> 子元素中。

<types> 元素是由 Types 段成。若料型要宣告,此段可以省略。在例 WSDL 中,用程式的型要宣告,但是使用 Types 段,以宣告本文件中描述的命名空。

<message> 元素是由 Messages 段所成。若作函, <message> 元素可定至函。<message> 元素中的每 <part> 子元素,都一。定入至一的 <message> 元素之中,位於自己 <message> 元素中的出分。同是入出的,在入出 <message> 元素中,各有自己相的 <part> 元素。根例,出 <message> 元素的名,如同「fooResponse」,以「Response」束。如同函需有名型一,每 <part> 元素也都有名型性。

若用於文件交,WSDL 可使用 <message> 元素,明交的文件。

<part> 元素的型可以是,XSD 基型、SOAP 定型 (soapenc)、WSDL 定型 (wsdl)、或 Types 段的定型。

在 PortTypes 段中,可以有零、一、或更多 <portType> 元素。由於抽象的 PortType 定,可置於不同的案中;因此在 WSDL 案中,可以有零 <portType> 元素。在上面的例中,即只有一 <portType> 元素。如所,<portType> 元素可在 <operation> 元素中,定一或多作。此例示一名「foo」的 <operation> 元素。此名函名相同。<operation> 元素可有一、二、或三子元素:即 <input>、<output> <fault> 元素。每 <input> <output> 元素中的息性,都照 Messages 段中的相 <message> 元素。因此,例中的整 <portType> 元素,相於下列的 C 函宣告:

int foo(int arg);

此例正足以明,相於 C,XML 是多冗的言。 (包括 <message> 元素在,此例共使用 12 行的 XML 行函宣告;而相同的作, C 只要一行即可。)

Bindings 段可有零、一或多 <binding> 元素。而其目的是,指定每 <operation> 呼叫回,在上送的方式。Services 段也可有零、一或多 <service> 元素。它所含的 <port> 元素,每都照 Bindings 段中的一 <binding> 元素。Bindings Services 段都是由,WSDL 文件的具描述所成。

命名空
在根元素 <definitions> 子元素 <schema> 中,都有命名空性:

<definitions name="FooSample"
targetNamespace="http://tempuri.org/wsdl/"
xmlns:wsdlns="http://tempuri.org/wsdl/"
xmlns:typens="http://tempuri.org/xsd"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:stk="http://schemas.microsoft.com/soap-toolkit/wsdl-extension"
xmlns="http://schemas.xmlsoap.org/wsdl/">

<types>
<schema targetNamespace="http://tempuri.org/xsd"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
elementFormDefault="qualified" >
</schema>
</types>

每命名空性,都命名空宣告一速法,以便在文件中使用。例如,「xmlns:xsd」可定一速法 (xsd),代表命名空 http://www.w3.org/2001/XMLSchema。如此一,稍後即可在文件中照此命名空;其方式很,只要在名前嵌入字首「xsd」,「xsd:int」即成合格的型名。一般的域定,皆可套用至此速字首。也就是,在一元素中所定的字首,在元素中使用。

使用命名空的原因何在?命名空的目的在避免命名突。若我建立了一路服,其 WSDL 案中含有一名「foo」的元素,而您想要我的路服,另一互性的服合起使用;假若有使用命名空,另一路服,在其 WSDL 案中,便不能使用「foo」名。除非在者的行中,都是指完全相同的西,否服不可以使用相同的名。但若使用不同的命名空,我路服的「foo」所代表的意,便另一路服的「foo」不同。在客端,您便必以嵌入字首 (prefixing 或 qualifying) 的方式,照我的「foo」。例如,若我宣告 http://www.infotects.com/fooService 的速法是 carlos, http://www.infotects.com/fooService#foo 完全合格的名,可以等於「carlos:foo」。注意,若使用 URI 作命名空,不但可保其特性,更可允在文件中使用定址器。URI 所指向的位址,不必真正的路位址。也可以使用 GUID 代替或充 URI。例如,GUID「335DB901-D44A-11D4-A96E-0080AD76435D」即是有效的命名空指示。

元素中所宣告的所有名,都附於 targetNamespace 性所宣告的命名空之下。在 WSDL 例案中,代表 <definitions> 的 targetNamespace 是 http://tempuri.org/wsdl。它所代表的意是,在此 WSDL 文件中宣告的所有名,都附於此命名空。由於 <schema> 元素有它自己的 targetNamespace 性,且其值 http://tempuri.org/xsd;所以在此 <schema> 元素中定的所有名,都是於此命名空,而不於主目命名空。

下一行程式位於 <schema> 元素中,它所宣告的是的命名空。中所有嵌入字首 (unqualified ) 的名,都於此命名空。

xmlns="http://www.w3.org/2001/XMLSchema"

SOAP 息
使用 WSDL 案的客端伺服端而言,它所代表的意之一是,可定上送的容。然 SOAP 使用的是,如 IP HTTP 等低次的通定,但此用程式可定,特定客端特定伺服端之所使用的高次通定。句,若以「echoInt」作例,使用回波回入整,、每的型、路的方式 (序列化) 等,便成一用程式特有的通定。指定通定的方式很多,但我使用 WSDL 才是最佳的方式。若就此而言,WSDL 便不只是一「介面合」,它更是一通定指定言。若超出了如 IP HTTP 等固定通定的,入用程式通定的域,WSDL 正好符合我所需。

WSDL 可以指定,SOAP 息是否符合 rpc 或文件式。正如例中所使用的一,rpc 式息的外就像是,一有零或多的函呼叫。文件式息扁平 (flatter) 且不需要那多的巢。下列 XML 息的送接收,是使用 MS SOAP Toolkit 2.0 (MSTK2) 的 SoapClient 物件,剖析 WSDL 例案的果。

自客端行函呼叫「foo(5131953)」:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SOAP-ENV:Envelope
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<m:foo xmlns:m="http://tempuri.org/message/">
<arg>5131953</arg>
</m:foo>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

伺服端所接收到的 (回):

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SOAP-ENV:Envelope
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<SOAPSDK1:fooResponse xmlns:SOAPSDK1="http://tempuri.org/message/">
<result>5131953</result>
</SOAPSDK1:fooResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

此函呼叫息其回,都是正有效的 XML。SOAP 息是由 <Envelope> 元素所成,其中含有一性的 <Header> 元素,至少一的 <body> 元素。送接收息者,在主 <Envelope> 元素中,都有只一 <Body> 元素。rpc 函呼叫息的主,有一依作名「foo」命名的元素,而回主中有一名「fooResponse」元素。 foo 元素有如例 WSDL 所示的一引,其名 <arg>。同地,fooResponse 也有一 <result>。在此重出的 WSDL Bindings 段中,注意 encodingStyle、信封、息命名空的指定方式。

<binding name="SimpleBinding" type="wsdlns:SimplePortType">
<stk:binding preferredEncoding="UTF-8" />
<soap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="foo">
<soap:operation
soapAction="http://tempuri.org/action/Simple.foo"/>
<input>
<soap:body use="encoded"
namespace="http://tempuri.org/message/"
encodingStyle=
"http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body use="encoded"
namespace="http://tempuri.org/message/"
encodingStyle=
"http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
</binding>

zGXHTML5中文学习网 - HTML5先行者学习网
zGXHTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助