Consider a scenario in which you need to use a Web service that performs complicated scientific calculations. You can search for this type of Web service in the UDDI directory. The directory provides you with a list of Web services that matches your search criteria. You can then register for the Web service that contains several Web methods to perform scientific 75 calculations. However, to use the Web methods, you need to have information about them, such as the description of the Web methods, the parameters that are passed to the Web methods, and so on. You also need to know the manner in which you can interact with the Web service. This information is provided in an XML format in a document called the WSDL document.
As stated earlier, the language that describes the Web service in a WSDL document is called WSDL. WSDL, a specification that the W3C developed, aims to provide a standard that can describe services. WSDL is based on XML; therefore, it provides an easy and a simple way to describe services in a text format. WSDL uses XML messages to allow structured communication between a set of endpoints. For example, an XML message can transfer between the client application and the service provider application. In this case, the client application and the service provider application are called endpoints.
The XML messages contain either procedure-oriented information or document-oriented information, which is bound to a network protocol, such as SOAP, and transferred between the endpoints. WSDL can also be extended to provide information about the endpoints and the XML messages that are exchanged between them.
WSDL provides an interface for a Web service. Therefore, when you create a Web service, you need to create a corresponding WSDL file. A WSDL file has an extension of .wsdl. We will now create a sample WSDL document for a credit card validation Web service. You can use this document as an interface for consuming the Web service.
A Sample WSDL Document
As discussed earlier, a WSDL document contains the description of a Web service, the Web methods, the way in which a client application can interact with the Web service, and so on. In addition, a client application can use WSDL to frame a request for the Web service. This information is contained in various components of a WSDL document. These components are mentioned in the following list:
So that you can understand the preceding components, we will create a sample WSDL document for a Web service that validates the credit card details of a customer. When you create a Web service by using Visual Studio .NET, the description of the Web service is created automatically. The description of the Web service has the same name as that of the Web service: CCValidate.wsdl.
In this chapter, we have created a WSDL document in Visual Studio .NET. However, you can create a WSDL document in other ways as well. You will learn about the ways to create a WSDL document in the next projects of this book.
When you open the CCValidate.wsdl document, the file contains the following code:
Now we will discuss the components of a WSDL file with reference to the preceding code.
The definitions Element
Because WSDL is an XML document, it begins with the definition for the XML version that is used in the document. In addition, you need to include the definitions for all namespaces in the document. To include the definitions for the namespaces, you use the definitions element. The definitions element is the root element for all elements in the WSDL document. The syntax for the definitions element is shown in the following code snippet:
The previous code includes the definitions element that contains the declarations for including the namespaces in the WSDL document. You can provide a name to the definitions by using the name attribute of the definitions element. For example, you can name the definitions in the previous code as CreditCard by dding the following code:<definitions name="CreditCard"--------</definitions>
Next, the URLs of the namespaces that you need to include in the WSDL document are added using the xmlns keyword. You can also provide a prefix to the namespace, as shown:xmlns:soap=http://schemas.xmlsoap.org/wsdl/soap/
In the preceding code, soap is the prefix that is assigned to thehttp://schemas.xmlsoap.org/wsdl/soap/ namespace.
In addition to adding namespaces to the WSDL document, you can specify a target namespace by using the target Namespace element. A target namespace uniquely identifies a WSDL document. Therefore, the same target namespace cannot be present in any other WSDL document. In addition, you can specify only one target namespace for a WSDL document. The targetNamespace element takes the name of the Web service as its value.
The types Element
After including the namespaces in the WSDL document, you need to include schemas or references to schemas. You can do this by using the types element. The syntax for the types element is as shown:
As you can see in the preceding syntax, the types element is included within the definitions element of the WSDL document.
A WSDL document contains abstract data type definitions that are required for exchanging messages between endpoints. These data type definitions are included in the types element.
The abstract data type definitions that you define in the types element can be included in multiple WSDL documents.
To include the data type definitions, WSDL uses XSD because XSD is used extensively as a data type definition mechanism for SOAP. In addition, XSD supports a large set of data types, which you can include in the WSDL document. You learned about the data types that XSD supports "Basics of Web Services."
The types element is optional. In addition, you can include only one types element in a WSDL document. However, the types element can, in turn, contain several schemas, as shown in the following code:
The preceding code contains definitions for abstract data types that can be serialized as a network protocol. For example, the data types that map to the messages can be serialized in the form of a SOAP message or an HTTP request.
Now we will discuss the statements in the preceding code. The preceding code uses the schema element that contains the target Namespace attribute to specify the target namespace for the WSDL document.
Next, the code uses element to declare an element with a name validateCC. When you define an element, you need to specify its data type. The validateCC element is a complex data type. You can specify an element as a complex data type by using the complex Type element.
The sequence element defines the sequence of the child elements that are declared as complex data types. In this case, two string type child elements with the names CardType and CardNumber are declared. These elements represent the input values that the user needs to provide to validate the credit details.
As you can see, the child element takes two attributes, minOccurs and maxOccurs. The minOccurs attribute specifies the minimum number of times that the element can appear in the containing element validateCC. To make this element optional, you can specify a value of 0. Similarly, the max Occurs attribute specifies the maximum number of times that the element can appear in the containing element validateCC. Because the CardType and CardNumber elements can accept only one value, the max Occurs attribute is set to 1.
Next, the code includes another complex type element with the name validateCCResponse. This element contains a Boolean type child element with the name validateCCResult. This element represents the value that the Web service returns and can be present only once in the containing element.
In addition to the validateCC and validateCCResponse elements, the types element declares a Boolean type element with the name boolean.
The message Element
As you know, the information is exchanged in the form of messages between two endpoints. Therefore, after declaring the data types of the messages in the types element, you need to define the structure of the message. Messages are defined using the message element. The syntax of the message element is as follows:
Messages represent an abstract definition of the data that you need to transfer between two endpoints. In addition, the message element defines the structure of the messages. To do this, the message element consists of a number of parts. Parts are logical units containing information that you need to communicate, and they are associated with the type systems that you define in the types element. In addition to associating a part with the type system that is defined in the types element, you can associate a part with the type system that is defined in other XML schema files or XML data types schema. To associate a part with a type system, you use the message-typing attribute.
You can use the part element to define a part in a message. A message that contains a part element is shown here:
The preceding code uses the message element to define the message validateCCSoapIn. The WSDL document contains a message definition for the messages that are transferred using SOAP, HTTP GET, and HTTP POST. In addition, each In message that is defined for a protocol has a corresponding Out message. Therefore, the previous code contains six message declaration statements—two for each protocol.
The validateCCSoapIn message contains a part called parameters. The validateCC element that is defined in the types element is passed as a parameter to the parameters part. The parameters part is associated with the complex type that is defined in the types element.
The validateCCSoapIn message has a corresponding validateCCSoapOut message that contains the parameters part. The validateCCResponse element that represents the value that is returned by the Web service is passed as a parameter to the validateCCSoapOut message.
As you can see, the part declaration statement contains the prefix s0, which indicates that the WSDL document is created using Visual Studio .NET.
Similarly, the validateCCHttpGetIn and validateCCHttpGetOut messages are defined for the HTTP GET protocol. The validateCCHttpGetIn message contains two string type parts: CardType and CardNumber. In addition, the validateCCHttpGetOut message contains a Boolean type part, Body, which represents the value that the Web service returns.
Similarly, Visual Studio .NET creates a code for the HTTP POST protocol, as shown in the preceding code.
The operation Element
As you already know, a Web service contains Web methods that you can use to perform a specific operation. A client application can request these operations and then execute them. Therefore, after you define messages, you need to associate them with the corresponding operation. To do this, you use the operation element, which, in turn, is included in the portType element. You will learn about the portType element in the later section titled "The portType Element."
The syntax of the operation element is as shown:
Similar to the message element, you need to have operations for all network protocols, such as SOAP, HTTP GET, and HTTP POST. The operation element has the input and output elements. The input element takes the input message as the value, and the output element takes the output message as the value.
For example, an operation with the name validateCC is created. The operation has an input element. The value assigned to the input element is validateCCSoapIn message. In addition, the operation element contains an output element. The value assigned to the output element is validateCCSoapOut message. The code for the operation element is as shown:
A WSDL document supports four types of operations. These operations are discussed in the following list:
For example, an operation element that contains only the input element is a one-way operation. The following code snippet shows an example of a one-way operation.
This operation is not commonly used in the Web services scenario. The example of this type of operation is shown in the following code:
The portType Element
You can combine the operations in WSDL document to form a set of operations called a port type. You can declare port types by using the portType element. In addition, you can specify a name for the port type by using the name attribute of the portType element. For example, you can combine the input and output elements of the validateCC operation in a port type, CCValidatorSoap. The code for the CCValidatorSoap port type is as shown:
The preceding code declares a port type with the name CCValidatorSoap, which contains the validateCC operation. The CCValidatorSoap port type is used to bind the operation to a protocol, such as SOAP, HTTP GET, and HTTP POST.
The binding Element
After creating a port type, it needs to be associated with a protocol, such as SOAP. This is called binding. To bind a port type with a protocol, you use the binding element. In addition, you can bind a port type to any number of protocols. For example, you can bind the same port type to the SOAP, HTTP GET, and HTTP POST protocols. These protocols, in turn, transfer the message from one endpoint to another.
Consider the following code snippet for the binding element:
The preceding code creates a binding with the name CCValidatorSoap. The name attribute specifies a unique name for the binding. In addition, you need to specify the port type for the binding element. You can do this by using the type attribute.
As you can see, the binding element contains many extensibility elements. These extensibility elements specify additional information about the service that the WSDL document defines. The transport element that is used in the preceding code is an example of an extensibility element. The transport element specifies the transport protocol.
In addition, the binding element includes a style element that specifies the style that is used to serialize a message to SOAP. The style can be either RPC based or document based. In the preceding code, the document-based serialization is used. In the document based serialization, messages are serialized by using a standard schema.
The binding element also includes the operation element, which specifies additional information about the operations. This information includes the style of the messages within the operation. You can specify the style by using the style attribute, which takes the value rpc or document. In addition, you can specify the soapAction header field in the soap:operation element. The soap Action header field provides information about the way in which a SOAP request is processed.
When you create a message, it needs to be associated with a protocol. This means that a message is serialized in the form of either a SOAP request or a response. Therefore, you need to specify the encoding form used for the input and output elements of a message. You can do this by using the soap:body element.
soap:body is another extensibility element that is used inside the binding element. Each input and output element in a message has a different soap:body element that is defined.
For example, you need to include the soap:body element for the input and output elements of the validateCC operation as follows:
The soap:body element contains a use attribute that specifies the encoding form for a message. The use attribute can take two values: literal or encoded. The following list discusses the encoding forms in detail:
The port Element
A binding is located at an individual endpoint. The port element, which is contained within the service element, specifies the address of a binding. We will explain the service element in the next section, "The service Element."
While defining a port, you need to specify a unique name. To do so, you use the name attribute of the port element. In addition, the port element contains an extensibility element: soap:address. This element binds the port to an address of an endpoint, which is a URI of an endpoint. The users can connect to the service by using the URI. Following is an example of a port element:
The preceding code creates a port for each binding that corresponds to the SOAP, HTTP POST, and HTTP GET protocols. Next, the soap:address element binds the port to the corresponding URIs.
The service Element
As you can see in the previous code, a single WSDL document might contain several ports. Therefore, you need to combine these ports. You can define a service to combine the ports in a WSDL document. In addition, you can specify a unique name for this set of ports. A service is defined using the service element. The syntax of the service element is as shown:
The service element contains ports that correspond to each of the protocols. However, you should ensure that all ports expose the same functionality. The elements that are used in a WSDL document are summarized in Table.
The Elements that are Used in a WSDL Document
As stated earlier, we created the Web service that corresponds to the CCValidate.wsdl document in Visual Studio .NET. To see the document in Visual Studio .NET, refer to Figure
The WSDL Document in Visual Studio .NET.
XML Related Interview Questions
|Soap Tool Interview Questions||HTML Interview Questions|
|PHP Interview Questions||ASP.NET Interview Questions|
|PHP5 Interview Questions||Java Interview Questions|
|CSS Interview Questions||XSLT Interview Questions|
|Java XML Interview Questions||XMLHttpRequest (XHR) Interview Questions|
|ebXML Interview Questions||XML DOM Interview Questions|
|XML-RPC Interview Questions||XSD Interview Questions|
|Soap Web Services Interview Questions||XSL Interview Questions|
|Xml Publisher Interview Questions|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.