Master C# Logo banner
Welcome to MasterCSharp.com - Master C#, the easy way... - by Saurabh Nandu

 


Proverb Web Service - Understanding the Web Service WSDL - Part 4

Add Comment
 

 
<div align="center"> <table class="outline" width="90%"> <tr> <td class="outline" width="12%"><b>SDK</b></td> </tr> <tr> <td class="outline" width="12%">Beta2</td> </tr> </table> </div> <p><span class="wboxheado">Introduction</span><br> In the previous few articles in the <a class="wbox" target="_blank" href="article.aspx?ArticleID=63&&TopicID=7"> Proverb Web Service</a> series I have shown you how to create the <b>Proverb Web Service</b>, as well as I showed you how to create a few clients that consume the Proverb web service. While creating these clients I took it for granted that the service was created by me and I knew the details of how the web service was implemented i.e. I knew the exact methods to call, the method parameters, the data-type of these parameters as well as the return value data-type. <br> In case I did not know how the web service was implemented and I did not have tools like WSDL.EXE that auto-generate proxy classes, then it might be a tuff task to consume this web service. <br> In this article I have a look in to <b>WSDL</b> (web service Discovery Language) of the Proverb web service, and try to make sense out of it :) !!</p> <p><span class=wboxheado>WSDL</span><br> WSDL (web service Discovery Language) is again a web standard maintained by <b>W3C</b> ( <a class="wbox" target="_blank" href="http://www.w3.org/TR/wsdl"> www.w3.org/TR/wsdl</a> ), currently in version <i>1.1</i>. Similar to everything else related with web services, even WSDL is <i>XML</i> formatted (you guessed this one, right??). WSDL files are used to describe the methods exposed by the web service, the protocols you can use to call those methods, the method parameters, the data-type of the parameters and the return value's data-type. In short WSDL contains all the metadata that you might need in order to create clients for consuming the web service. Even though WSDL is a very complex and evolving standard, .NET developers have no trouble implementing, since the .NET Platform generates the WSDL for each web service <b>automatically</b> !! (You can add this in your '<i>101 reasons to adopt the .NET Platform</i>' list :) !). Many other platforms require you to manually code the WSDL file for your web services! </p> <p>To access the WSDL of any web service created on the .NET Platform just append the <b>?WSDL</b> argument after the web service URL in any browser and you will be presented with the auto-generated WSDL file for the web service. For example if you installed the Proverb Web Service with the path <i>http://localhost/ProverbService/ProverbService.asmx</i>&nbsp; append the <i>?WSDL</i> argument to this path in your browser to view the WSDL document for the web service i.e. call the new URL <i>http://localhost/ProverbService/ProverbService.asmx?WSDL</i> . In case you don't have the Proverb web service installed on you computer, <a class="wbox" target="_blank" href="live/proverbservice1/proverbservice.asmx?WSDL"> click here</a> to view the WSDL of the Proverb Web Service hosted on my server. I will keep referring to it again throughout the article so better keep the WSDL file open and have a good look at it!</p> <p><span class=wboxheado>WSDL Sections</span><br> The WSDL of any web service is an XML file that will containing at several elements nested within the root element called <b>definitions</b>. The table below summarizes the use of some common important elements present in all WSDL files..</p> <div align="center"> <center> <table cellpadding="1" cellspacing="2" width="90%" class="outline" > <tr> <td width="33%" class="outline"><b>Element</b></td> <td width="67%" class="outline"><b>Description</b></td> </tr> <tr> <td width="33%" class="outline">types</td> <td width="67%" class="outline">This element defines the schema of the data-types used in the web service. Both input parameters and return values of each method are defined in this section using any of the existing XML data-type description standards like XSD, XML Schema etc.</td> </tr> <tr> <td width="33%" class="outline">messages</td> <td width="67%" class="outline">This element defines the input and out parameters for each of the methods the web service exposes. This section refers to the data-types&nbsp; defined in the above types section.</td> </tr> <tr> <td width="33%" class="outline">operation</td> <td width="67%" class="outline">This element defines the operations your web services supports.</td> </tr> <tr> <td width="33%" class="outline">portType</td> <td width="67%" class="outline">This element ties the different access methods with the messages elements defined above.</td> </tr> <tr> <td width="33%" class="outline">binding</td> <td width="67%" class="outline">This element defines the complete protocols that can be used to access the Web Method (Http POST, Http GET, SOAP) and encoding to be used for each of these protocols. </td> </tr> <tr> <td width="33%" class="outline">port</td> <td width="67%" class="outline">This elements contains the URL for accessing the web service.</td> </tr> <tr> <td width="33%" class="outline">service</td> <td width="67%" class="outline">This element is a collection of the port elements.</td> </tr> </table> </center> </div> <p><span class=wboxheado>Analyzing the Proverb web service WSDL</span><br> I guess you already have the WSDL of the Proverb Service on your desktop, let's understand what information the different sections of this file gives us.</p> <p><span class=wboxhead>1) definitions (root element)</span></p> <table cellpadding="1" cellspacing="2" width="100%" class="code"> <tr> <td width="100%"><pre> </pre> &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt; <br> &lt;definitions xmlns:s=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns:http=&quot;http://schemas.xmlsoap.org/wsdl/http/&quot; xmlns:mime=&quot;http://schemas.xmlsoap.org/wsdl/mime/&quot; xmlns:tm=&quot;http://microsoft.com/wsdl/mime/textMatching/&quot; xmlns:soap=&quot;http://schemas.xmlsoap.org/wsdl/soap/&quot; xmlns:soapenc=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot; xmlns:s0=&quot;http://www.mastercsharp.com/WebService&quot; targetNamespace=&quot;http://www.mastercsharp.com/WebService&quot; xmlns=&quot;http://schemas.xmlsoap.org/wsdl/&quot;&gt; <br> <br> &lt;/definitions&gt; </td> </tr> </table><b>Listing 1: </b>definition element <p>You can see from the above listing 1, that the definitions element is the root element and it defines a lot of <i>XML namespaces</i> that are used in the XML formatted WSDL document. <br> Observe that since I had defined the the <b>Namespace</b> property of the <b>WebServiceAttribute</b> applied to the <b>Proverbs</b> class as <i> http://www.mastercsharp.com/WebService</i>, the WSDL for this service defines a XML Namespace with the same URL where all the custom web service related data-types will be placed under i.e. <i> xmlns:s0=&quot;http://www.mastercsharp.com/WebService&quot;</i>. </p> <p><span class=wboxhead>2) types element</span></p> <table cellpadding="1" cellspacing="2" width="100%" class="code"> <tr> <td width="100%"> <pre>&lt;types&gt; &lt;s:schema attributeFormDefault=&quot;qualified&quot; elementFormDefault=&quot;qualified&quot; targetNamespace=&quot;http://www.mastercsharp.com/WebService&quot;&gt; &lt;s:element name=&quot;GetProverb&quot;&gt; &lt;s:complexType /&gt; &lt;/s:element&gt; &lt;s:element name=&quot;GetProverbResponse&quot;&gt; &lt;s:complexType&gt; &lt;s:sequence&gt; &lt;s:element minOccurs=&quot;1&quot; maxOccurs=&quot;1&quot; name=&quot;GetProverbResult&quot; nillable=&quot;true&quot; type=&quot;s:string&quot; /&gt; &lt;/s:sequence&gt; &lt;/s:complexType&gt; &lt;/s:element&gt; &lt;s:element name=&quot;AddProverb&quot;&gt; &lt;s:complexType&gt; &lt;s:sequence&gt; &lt;s:element minOccurs=&quot;1&quot; maxOccurs=&quot;1&quot; name=&quot;userProverb&quot; nillable=&quot;true&quot; type=&quot;s:string&quot; /&gt; &lt;/s:sequence&gt; &lt;/s:complexType&gt; &lt;/s:element&gt; &lt;s:element name=&quot;AddProverbResponse&quot;&gt; &lt;s:complexType&gt; &lt;s:sequence&gt; &lt;s:element minOccurs=&quot;1&quot; maxOccurs=&quot;1&quot; name=&quot;AddProverbResult&quot; nillable=&quot;true&quot; type=&quot;s:string&quot; /&gt; &lt;/s:sequence&gt; &lt;/s:complexType&gt; &lt;/s:element&gt; &lt;s:element name=&quot;string&quot; nillable=&quot;true&quot; type=&quot;s:string&quot; /&gt; &lt;/s:schema&gt; &lt;/types&gt;</pre> </td> </tr> </table><b>Listing 2:</b> types element <p>Listing 2, displays the <b>types</b> element for the Proverb Web Service WSDL, the <i>types</i> element defines the <b>XML schema</b> for the parameters used during method calls. WSDL treats the input parameters and return values of a method call as separate entities.&nbsp; The input parameters are named with the same name as the method name. Hence in the above snip, the <b>element</b> with <b>name</b> attribute as <b>GetProverb</b> represents the input parameter for the method <b>GetProverb</b>, but since this method takes no parameters this element has an empty <b>complexType</b> element. Similarly, the <b>element</b> with name attribute as <b>AddProverb</b> represents the input parameter from the method <b>AddProverb</b>. Also this method takes a string <b>userProverb</b> as the input parameter hence even the WSDL <b>AddProverb</b> element defines a child element of type <b>string</b>. The <b>minOccurs</b> and <b>maxOccurs</b> attributes indicate that this element is needed and it can occur only a single time.<p>The return parameters of the methods are marked with the method name followed by the word <b>Response</b> so the elements named <b>GetProverbResponse</b> and <b>AddProverbResponse</b> define the schema for the return values for the methods <i>GetProverb</i> and <i>AddProverb</i> respectively. Both these methods return a <i>string</i> type. Lastly, the schema from the <i>string</i> type is defined. <p><span class=wboxhead>3) message element</span></p><table cellpadding="1" cellspacing="2" width="100%" class="code" > <tr> <td width="100%"> <pre>&lt;message name=&quot;GetProverbSoapIn&quot;&gt; &lt;part name=&quot;parameters&quot; element=&quot;s0:GetProverb&quot; /&gt; &lt;/message&gt; &lt;message name=&quot;GetProverbSoapOut&quot;&gt; &lt;part name=&quot;parameters&quot; element=&quot;s0:GetProverbResponse&quot; /&gt; &lt;/message&gt; &lt;message name=&quot;AddProverbSoapIn&quot;&gt; &lt;part name=&quot;parameters&quot; element=&quot;s0:AddProverb&quot; /&gt; &lt;/message&gt; &lt;message name=&quot;AddProverbSoapOut&quot;&gt; &lt;part name=&quot;parameters&quot; element=&quot;s0:AddProverbResponse&quot; /&gt; &lt;/message&gt; &lt;message name=&quot;GetProverbHttpGetIn&quot; /&gt; &lt;message name=&quot;GetProverbHttpGetOut&quot;&gt; &lt;part name=&quot;Body&quot; element=&quot;s0:string&quot; /&gt; &lt;/message&gt; &lt;message name=&quot;AddProverbHttpGetIn&quot;&gt; &lt;part name=&quot;userProverb&quot; type=&quot;s:string&quot; /&gt; &lt;/message&gt; &lt;message name=&quot;AddProverbHttpGetOut&quot;&gt; &lt;part name=&quot;Body&quot; element=&quot;s0:string&quot; /&gt; &lt;/message&gt; &lt;message name=&quot;GetProverbHttpPostIn&quot; /&gt; &lt;message name=&quot;GetProverbHttpPostOut&quot;&gt; &lt;part name=&quot;Body&quot; element=&quot;s0:string&quot; /&gt; &lt;/message&gt; &lt;message name=&quot;AddProverbHttpPostIn&quot;&gt; &lt;part name=&quot;userProverb&quot; type=&quot;s:string&quot; /&gt; &lt;/message&gt; &lt;message name=&quot;AddProverbHttpPostOut&quot;&gt; &lt;part name=&quot;Body&quot; element=&quot;s0:string&quot; /&gt; &lt;/message&gt;</pre> </td> </tr> </table><b>Listing 3:</b> message element <p> Listing 3, above defines the WSDL <b>message</b> element. You will observe from the above listing that the <i>message</i> element defines the <i>protocols</i> that can be used to interact with the Web Methods. The method call parameters are post-fixed with <b>In</b> while the return values is post-fixed with <b>Out</b>.&nbsp; The <b>part</b> child element defines the schema to be used while calling these methods, these schema elements have already defined in the <b>types</b> element (listing 2). The <b>name</b> attribute of the <b>part</b> element defines the <i>name of the parameter</i> if any. For example under the <i>message</i> element with <i>name</i> attribute as <b>AddProverbHttpGetIn</b> the <i>name</i> attribute of the <i>part</i> element is <b>userProverb</b> so if you want to call this web method using <i>HTTP GET</i> request you need to pass along a <i>userProverb</i> parameter so a sample HTTP GET request might be<br> <i>http://localhost/ProverbService/ProverbService.asmx/AddProverb?userProverb=sample%20proverb</i><p> Another observation is that, for each of the web methods defined in the Proverb Web Service, the WSDL has 4 <b>message</b> elements defined. This might not be true for all web services. There are three protocols for calling Web Methods; <b>HTTP GET</b> / <b>HTTP POST</b> and <b>SOAP</b>. Web Methods that deal with complex data-types like DataSet's cannot be called over HTTP GET / HTTP POST since these these data-types cannot be expressed correctly. The .NET runtime automatically senses this can generates only the supported message elements.<p> <span class=wboxhead>4) portType element</span></p><table cellpadding="1" cellspacing="2" width="100%" class="code" > <tr> <td width="100%"><pre>&lt;portType name=&quot;ProverbsSoap&quot;&gt; &lt;operation name=&quot;GetProverb&quot;&gt; &lt;input message=&quot;s0:GetProverbSoapIn&quot; /&gt; &lt;output message=&quot;s0:GetProverbSoapOut&quot; /&gt; &lt;/operation&gt; &lt;operation name=&quot;AddProverb&quot;&gt; &lt;input message=&quot;s0:AddProverbSoapIn&quot; /&gt; &lt;output message=&quot;s0:AddProverbSoapOut&quot; /&gt; &lt;/operation&gt; &lt;/portType&gt; &lt;portType name=&quot;ProverbsHttpGet&quot;&gt; &lt;operation name=&quot;GetProverb&quot;&gt; &lt;input message=&quot;s0:GetProverbHttpGetIn&quot; /&gt; &lt;output message=&quot;s0:GetProverbHttpGetOut&quot; /&gt; &lt;/operation&gt; &lt;operation name=&quot;AddProverb&quot;&gt; &lt;input message=&quot;s0:AddProverbHttpGetIn&quot; /&gt; &lt;output message=&quot;s0:AddProverbHttpGetOut&quot; /&gt; &lt;/operation&gt; &lt;/portType&gt; &lt;portType name=&quot;ProverbsHttpPost&quot;&gt; &lt;operation name=&quot;GetProverb&quot;&gt; &lt;input message=&quot;s0:GetProverbHttpPostIn&quot; /&gt; &lt;output message=&quot;s0:GetProverbHttpPostOut&quot; /&gt; &lt;/operation&gt; &lt;operation name=&quot;AddProverb&quot;&gt; &lt;input message=&quot;s0:AddProverbHttpPostIn&quot; /&gt; &lt;output message=&quot;s0:AddProverbHttpPostOut&quot; /&gt; &lt;/operation&gt; &lt;/portType&gt;</pre></td> </tr> </table><b>Listing 4:</b> portType element <p> The <b>portType</b> element shown in listing 4, is used to bind supported protocols with the <i>message</i> element defined in listing 3. The <b>name</b> attribute of the <i>portType</i> element defines the supported protocols, while the <b>name</b> attribute of the child <b>operation</b> element contains the actual name of the Web Methods. The <b>input</b> and <b>output</b> sub-elements bind the <i>message</i> element required during the method call and method return respectively. The <b>message</b> attribute of the <i>input</i> and <i>output</i> elements directly corresponds with the <i>message</i> element defined in listing 3.<p> <span class=wboxhead>5) binding element</span></p><table cellpadding="1" cellspacing="2" width="100%" class="code"> <tr> <td width="100%"> <pre>&lt;binding name=&quot;ProverbsSoap&quot; type=&quot;s0:ProverbsSoap&quot;&gt; &lt;soap:binding transport=&quot;http://schemas.xmlsoap.org/soap/http&quot; style=&quot;document&quot; /&gt; &lt;operation name=&quot;GetProverb&quot;&gt; &lt;soap:operation soapAction=&quot;http://www.mastercsharp.com/WebService/GetProverb&quot; style=&quot;document&quot; /&gt; &lt;input&gt; &lt;soap:body use=&quot;literal&quot; /&gt; &lt;/input&gt; &lt;output&gt; &lt;soap:body use=&quot;literal&quot; /&gt; &lt;/output&gt; &lt;/operation&gt; &lt;operation name=&quot;AddProverb&quot;&gt; &lt;soap:operation soapAction=&quot;http://www.mastercsharp.com/WebService/AddProverb&quot; style=&quot;document&quot; /&gt; &lt;input&gt; &lt;soap:body use=&quot;literal&quot; /&gt; &lt;/input&gt; &lt;output&gt; &lt;soap:body use=&quot;literal&quot; /&gt; &lt;/output&gt; &lt;/operation&gt; &lt;/binding&gt; &lt;binding name=&quot;ProverbsHttpGet&quot; type=&quot;s0:ProverbsHttpGet&quot;&gt; &lt;http:binding verb=&quot;GET&quot; /&gt; &lt;operation name=&quot;GetProverb&quot;&gt; &lt;http:operation location=&quot;/GetProverb&quot; /&gt; &lt;input&gt; &lt;http:urlEncoded /&gt; &lt;/input&gt; &lt;output&gt; &lt;mime:mimeXml part=&quot;Body&quot; /&gt; &lt;/output&gt; &lt;/operation&gt; &lt;operation name=&quot;AddProverb&quot;&gt; &lt;http:operation location=&quot;/AddProverb&quot; /&gt; &lt;input&gt; &lt;http:urlEncoded /&gt; &lt;/input&gt; &lt;output&gt; &lt;mime:mimeXml part=&quot;Body&quot; /&gt; &lt;/output&gt; &lt;/operation&gt; &lt;/binding&gt; &lt;binding name=&quot;ProverbsHttpPost&quot; type=&quot;s0:ProverbsHttpPost&quot;&gt; &lt;http:binding verb=&quot;POST&quot; /&gt; &lt;operation name=&quot;GetProverb&quot;&gt; &lt;http:operation location=&quot;/GetProverb&quot; /&gt; &lt;input&gt; &lt;mime:content type=&quot;application/x-www-form-urlencoded&quot; /&gt; &lt;/input&gt; &lt;output&gt; &lt;mime:mimeXml part=&quot;Body&quot; /&gt; &lt;/output&gt; &lt;/operation&gt; &lt;operation name=&quot;AddProverb&quot;&gt; &lt;http:operation location=&quot;/AddProverb&quot; /&gt; &lt;input&gt; &lt;mime:content type=&quot;application/x-www-form-urlencoded&quot; /&gt; &lt;/input&gt; &lt;output&gt; &lt;mime:mimeXml part=&quot;Body&quot; /&gt; &lt;/output&gt; &lt;/operation&gt; &lt;/binding&gt;</pre> </td> </tr> </table><b>Listing 5:</b> binding element <p> The <b>binding</b> element defines the protocols supported by the Web Methods and their encoding-format. The <b>name</b> attribute of the <i>binding</i> element defines the protocol while the <b>type</b> attribute connects to the respective <i>portType</i> element defined in listing 4. Rest of the elements are specific to the protocol being defined and the encoding format required for interacting with the specified protocol.<p> <span class=wboxhead>6) service element</span></p><table cellpadding="1" cellspacing="2" width="100%" class="code"> <tr> <td width="100%"> <pre>&lt;service name=&quot;Proverbs&quot;&gt; &lt;port name=&quot;ProverbsSoap&quot; binding=&quot;s0:ProverbsSoap&quot;&gt; &lt;soap:address location=&quot;http://localhost/ProverbService/proverbservice.asmx&quot; /&gt; &lt;/port&gt; &lt;port name=&quot;ProverbsHttpGet&quot; binding=&quot;s0:ProverbsHttpGet&quot;&gt; &lt;http:address location=&quot;http://localhost/ProverbService/proverbservice.asmx&quot; /&gt; &lt;/port&gt; &lt;port name=&quot;ProverbsHttpPost&quot; binding=&quot;s0:ProverbsHttpPost&quot;&gt; &lt;http:address location=&quot;http://localhost/ProverbService/proverbservice.asmx&quot; /&gt; &lt;/port&gt; &lt;/service&gt; </pre> </td> </tr> </table><b>Listing 6:</b> service element <p> The <b>service</b> element in listing 6 defines the URL to access the Web Methods for the different protocols supported by the web service. The <b>binding</b> attribute of the child <b>port</b> element corresponds to the <i>binding</i> element defined in listing 6. This completes our analysis of the Proverb Web Service WSDL.<p> <span class=wboxheado>Putting it all Together</span><br> Honestly, how much of the above made sense ?? (Don't tell me it was a absolute bumper!)<br> Anyway, just to make things more clear lets take a small example. <br> Let's say we are presented with the WSDL of this Proverb Web Service and we are supposed call the <i>GetProverb</i> of the web service using <i>HTTP GET</i> to retrieve proverbs. Below is the list of steps in serial order that should be followed to understand how to communicate with the web service.<p> 1) The first step would be to examine the <b>service</b> element from listing 6 and note down that in order to to communicate with the web service we should communicate with the URL.<br> i.e. <i>http://localhost/ProverbService/proverbservice.asmx,</i>&nbsp; also note the the <b>binding</b> element i.e. <i>ProverbsHttpGet</i>.<p> 2) Now jump to <b>binding</b> element as in listing 5 and search of the <i>binding</i> element with the <b>name</b> attribute as <i>ProverbsHttpGet</i>. The first thing you should check here is that there exists a <b>operation</b> child element with the <i>name</i> attribute same as the Web Method name you intend to call i.e. <i>GetProverb</i> in this case. This is because if the Web Method does not support being called over HTTP GET then there will be no entry for the respective operation element. Since in this case the element exists, on examining its child elements we find that for calling the <i>GetProverb</i> method we need to append an additional location of <b>/GetProverb</b> after the web service URL. So the complete URL to calling the Web Method over HTTP GET becomes<br> <i>http://localhost/ProverbService/proverbservice.asmx/GetProverb</i>.<br> We also observe that the input parameters have to be <b>URL Encoded</b> and the return value is of the <b>MIME type mimeXML</b> and is returned as the <b>body</b> of the HTTP response.<p> 3) Switch to the <b>portType</b> element as in listing 4 and search for the <i>portType</i> element with the <b>name</b> attribute as <b>ProverbsHttpGet</b>. Here note down the <b>message</b> attribute for the <b>input</b> and <b>output</b> elements i.e. <i>GetProverbHttpGetIn</i> and <i>GetProverbHttpGetOut</i> respectively.<p> 4) Move up to the <b>message</b> element and search for <b>message</b> elements with the <b>name</b> attribute as <i>GetProverbHttpGetIn</i> and <i>GetProverbHttpGetOut</i>. We find that the <i>message</i> element with the <i>name</i> attribute <i>GetProverbhttpGetIn</i> has no child elements, this signifies that the <i>GetProverb</i> method has no method call parameters. While the <i>message</i> element with the <i>name</i> attribute <i>GetProverbHttpGetOut</i> has one child element called <b>part</b> with <b>element</b> attribute as <b>string</b> signifying that the <i>GetProverb</i> method returns <i>one</i> return value of the data-type <i>string</i>.<p> 5) Lastly, you can see the schema for the input and output web method parameters from the <b>types</b> element as shown in listing 2. <br> Equipped with this knowledge, calling the Web Method should be very easy!&nbsp; <p> <span class=wboxheado>Conclusion</span><br> In this article I touched upon the various elements that are contained within the WSDL of a web service. You should have also learned to interpret and understand the composition of the WSDL and its importance in describing a web service.

Comments

Add Comment