Structure of a WSDL Document
A WSDL (Web Services Description Language) document plays a vital role in facilitating web services by detailing how the service can be called, what parameters it expects, and what data structures will be returned. Understanding its structure is essential for developers working with WCF (Windows Communication Foundation) and for anyone engaged in service-oriented architecture (SOA). In this article, we will break down the components of a WSDL document and explain the role of each section, allowing you to fully grasp how services operate in the .NET ecosystem.
WSDL Document Overview
WSDL is an XML-based language primarily used for describing services in a network. The WSDL document generally consists of several key parts, each serving a distinct purpose. By dissecting the architecture of a WSDL document, we can better understand how these services are defined.
Components of a WSDL Document
A WSDL document is structured with the following main components:
- Definitions
- Types
- Messages
- Port Types
- Bindings
- Services
Let’s explore each of these components in detail.
1. Definitions
The <definitions> element is the root element of a WSDL document. It establishes the namespace and the overall context for the service description. Here’s what typically goes into this section:
- xmlns: This attribute specifies the XML namespace for the WSDL elements.
- targetNamespace: This attribute defines the namespace where the elements of the service will reside.
- name: The name of the WSDL document and service being defined.
Here’s an example:
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:tns="http://example.com/MyService"
targetNamespace="http://example.com/MyService"
name="MyService">
2. Types
The <types> section is crucial for defining the data structures used by the service. This area typically includes XML schema definitions (XSD), which describe the data types being employed within the messages exchanged by the client and the server.
Example of a types section:
<types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://example.com/MyService">
<xsd:element name="GetDataRequest">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="id" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="GetDataResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</types>
3. Messages
The <message> element defines a what a particular operation can send or receive, essentially outlining the messages exchanged between the service provider and the consumer. Each message can consist of multiple parts.
Here’s a breakdown:
- name: The name of the message.
- part: Specifies the individual components of a message, including names and data types.
Example:
<message name="GetDataRequestMessage">
<part name="parameters" element="tns:GetDataRequest"/>
</message>
<message name="GetDataResponseMessage">
<part name="parameters" element="tns:GetDataResponse"/>
</message>
4. Port Types
The <portType> element acts as an abstract interface that consists of a set of operations defined for the service, with each operation representing a single method offered by the service. This section is where we establish the connection between the service and the messages and define what operations are available.
Each operation specifies:
- name: The name of the operation.
- input: Refers to a message the service accepts.
- output: Refers to a message sent back in response.
Here’s an example:
<portType name="MyServicePortType">
<operation name="GetData">
<input message="tns:GetDataRequestMessage"/>
<output message="tns:GetDataResponseMessage"/>
</operation>
</portType>
5. Bindings
The <binding> element describes the technical details of how the service can communicate over a network. This includes information about the data format and protocol used for each operation. To further define the communication method, each binding type can be associated with various transport protocols, like SOAP, HTTP, or JMS.
Elements of this section:
- name: Name of the binding.
- type: The port type the binding is addressing.
Example with SOAP binding:
<binding name="MyServiceSoapBinding" type="tns:MyServicePortType">
<soap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="GetData">
<soap:operation soapAction="http://example.com/MyService/GetData"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
6. Services
Finally, the <service> element brings everything together, defining the actual endpoints for the service. It establishes the address where the service can be found and the binding that should be utilized.
Key aspects:
- name: The name of the service.
- port: Specifies the binding and the address.
Example:
<service name="MyService">
<port name="MyServicePort" binding="tns:MyServiceSoapBinding">
<soap:address location="http://example.com/MyService"/>
</port>
</service>
Putting It All Together
A complete WSDL document combines all these components to produce a cohesive description of the web service. Let’s take a look at what a fully structured WSDL document might look like, building off our previous snippets:
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:tns="http://example.com/MyService"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/MyService"
name="MyService">
<types>
<!-- Type definitions go here -->
</types>
<message name="GetDataRequestMessage">
<part name="parameters" element="tns:GetDataRequest"/>
</message>
<message name="GetDataResponseMessage">
<part name="parameters" element="tns:GetDataResponse"/>
</message>
<portType name="MyServicePortType">
<operation name="GetData">
<input message="tns:GetDataRequestMessage"/>
<output message="tns:GetDataResponseMessage"/>
</operation>
</portType>
<binding name="MyServiceSoapBinding" type="tns:MyServicePortType">
<soap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="GetData">
<soap:operation soapAction="http://example.com/MyService/GetData"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="MyService">
<port name="MyServicePort" binding="tns:MyServiceSoapBinding">
<soap:address location="http://example.com/MyService"/>
</port>
</service>
</definitions>
Conclusion
Understanding the structure of a WSDL document is essential for developers engaged in building and consuming web services. By breaking down its components, we see how they come together to provide a thorough description of the services available, the data types used, and how to interact with those services over the network.
As you venture further into the realms of WCF and web services, keep this structure in mind, and you’ll have a solid foundation to build from. Whether you’re defining your services or consuming them, knowledge of WSDL will be a crucial asset in your toolbox. So, dive in and explore the world of web services with confidence!