在.NET中三种方式生成WSDL的方法
11/10/2012 11:22:26 AM
WSDL是一个XML格式用以描述怎样调用具体的Web Service,Web Service提供什么样的Service等等。
在.NET中有三种方式生成WSDL:
1.在Web Service的URL后面加上WDSL需求,如下:
http://localhost/webExamples/simpleService.asmx?WSDL
2.使用disco.exe。在命令行中写下如下的命令:
disco http://localhost/webExamples/simpleService.asmx
3.使用System.Web.Services.Description命名空间下提供的类
每个 WSDL 文件的根元素都是 <definitions>,必须在其中提供服务的完整描述。首先,必须在 <definitions> 元素中提供各种名称空间的声明。
<definitions> 元素包含一个或多个 < portType > 元素,每个元素都是一系列 operation。可以将单个portType元素看作是将各种方法组成类的一个逻辑分组。应该将每个Types称为服务,因此整个 WSDL 文件将成为一个服务集合。
在每个服务内可以有几个方法或者 operation,WSDL 通过 <operation> 元素来引用它们。
下面是一个最简单的WSDL例子
<?xml version="1.0" encoding="UTF-8" ?>
<definitions name="MobilePhoneService"
targetNamespace="www.mobilephoneservice.com/MobilePhoneService-interface"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://www.mobilephoneservice.com/MobilePhoneService"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<portType name="MobilePhoneService_port">
<operation name="getListOfModels ">
.......
.......
</operation>
<operation name="getPrice">
.......
.......
</operation>
</portType>
</definitions>
Sample 1
定义好操作(或方法)以后,现在需要指定将向它们发送和从它们返回的参数。在 WSDL 术语中,所有参数称为“消息”。认为您是在递送消息而结果得到返回的消息是有用的。方法调用是这样一种操作:它准备返回“消息”来响应进入的消息。
<?xml version="1.0" encoding="UTF-8" ?>
<definitions name="MobilePhoneService" targetNamespace="http://www.mobilephoneservice.com/MobilePhoneService-interface"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://www.mobilephoneservice.com/MobilePhoneService"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<types>
<xsd:schema targetNamespace="http://www.mobilephoneservice.com/MobilePhoneService"
xmlns="http://www.w3.org/1999/XMLSchema/">
<xsd:complexType name="Vector">
<xsd:element name="elementData" type="xsd:String" />
<xsd:element name="elementCount" type="xsd:int" />
</xsd:complexType>
</xsd:schema>
</types>
<message name="ListOfPhoneModels">
<part name="models" type="tns:Vector">
</message>
<message name="PhoneModel">
<part name="model" type="xsd:String">
</message>
<message name="PhoneModelPrice">
<part name="price" type="xsd:String">
</message>
<portType name="MobilePhoneService_port">
<operation name="getListOfModels ">
<output message="ListOfPhoneModels"/>
</operation>
<operation name="getPrice">
<Input message="PhoneModel"/>
<output message="PhoneModelPrice"/>
</operation>
</portType>
</definitions>
Sample 2
WSDL 的任务是定义或描述 Web 服务,然后提供一个对外部框架的引用来定义 WSDL 用户将如何实现这些服务。可以将这个框架当作 WSDL 抽象定义和它们的实现之间的“绑定(binding)”。
WSDL 将指定能够访问 Web 服务实际实现的 SOAP 服务器,并且从那时起 SOAP 的整个任务就是将用户从 WSDL 文件带到它的实现。
WSDL binding 元素包含您将用于绑定用途的外部技术的声明。因为正在使用 SOAP,所以这里将使用 SOAP 的名称空间。WSDL 术语中,对外部名称空间的使用称为 extensibility 元素。
… …
<portType name="MobilePhoneService_port">
... ...
</portType>
<binding name="MobilePhoneService_Binding" type="MobilePhoneService_port">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
<operation name="getListOfModels ">
<soap:operation soapAction="urn:MobilePhoneService" />
<input>
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:MobilePhoneService" use="encoded" />
</input>
<output>
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:MobilePhoneService" use="encoded" />
</output>
</operation>
</binding>
… …
Sample 3
WSDL 需要一个附加步骤来创建该 WSDL 文件的概要。WSDL 将该文件称为 implementation 文件,当在 UDDI 注册中心发布 Web 服务时,会使用它。
<?xml version="1.0" encoding="UTF-8" ?>
<definitions name="MobilePhoneService"
targetNamespace="http://www.mobilephoneservice.com/MobilePhoneService"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://www.mobilephoneservice.com/MobilePhoneService"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<import location="http://localhost:8080/wsdl/MobilePhoneService-interface.wsdl" namespace=http://www.mobilephoneserviceservice.com/MobilePhoneService-interface/>
<service name="MobilePhoneService">
<documentation> Mobile Phone Information Service </documentation>
<port binding="MobilePhoneService_Binding" name="MobilePhoneService_ServicePort">
<soap:address location="http://localhost:8080/soap/servlet/rpcrouter" />
</port>
</service>
</definitions>
Refer to
1. Web service and the introduction of WSDL – BY Bilal Siddiqui.
2. MCSD 70-320 book