WSDL Example

 

Using Visual Studio .NET, I created this C# Web Service.

 

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Diagnostics;

using System.Web;

using System.Web.Services;

 

namespace COMP750

{

      [WebService(Namespace="http://localhost/COMP750/")]

      public class Example : System.Web.Services.WebService

      {

            // irrelevant code removed

 

            [WebMethod] // The WebMethod attribute declares that this method will

                                                // be available for remote access.

            public string HelloWorld()

            {

                  return "Hello World";

            }

 

            [WebMethod]

            public double toThe(float number, int n)

            {

                  double result = 1.0;

                  if (n > 0)

                  {

                        for (int i = 0; i < n; i++)

                        {

                              result *= number;

                        }

                  }

                  else

                        result = 0.0;

                  return result;

            }

      }

}

 

There are two methods:

HelloWorld which takes no arguments and returns a string.

toThe which takes a float and an int and returns a double.

 

When you invoke the HelloWorld service, the following messages are sent:

SOAP

The following is a sample SOAP request and response. The placeholders shown need to be replaced with actual values.

POST /COMP750/Service1.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://localhost/COMP750/HelloWorld"
 
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <HelloWorld xmlns="http://localhost/COMP750/" />
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
 
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <HelloWorldResponse xmlns="http://localhost/COMP750/">
      <HelloWorldResult>string</HelloWorldResult>
    </HelloWorldResponse>
  </soap:Body>
</soap:Envelope>

HTTP POST

The following is a sample HTTP POST request and response. The placeholders shown need to be replaced with actual values.

POST /COMP750/Service1.asmx/HelloWorld HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: length
 
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
 
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://localhost/COMP750/">string</string>

 

When you invoke the toThe service, the following messages are sent:

SOAP

The following is a sample SOAP request and response. The placeholders shown need to be replaced with actual values.

POST /COMP750/Service1.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://localhost/COMP750/toThe"
 
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <toThe xmlns="http://localhost/COMP750/">
      <number>float</number>
      <n>int</n>
    </toThe>
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
 
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <toTheResponse xmlns="http://localhost/COMP750/">
      <toTheResult>double</toTheResult>
    </toTheResponse>
  </soap:Body>
</soap:Envelope>

HTTP POST

The following is a sample HTTP POST request and response. The placeholders shown need to be replaced with actual values.

POST /COMP750/Service1.asmx/toThe HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: length
 
number=string&n=string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
 
<?xml version="1.0" encoding="utf-8"?>
<double xmlns="http://localhost/COMP750/">double</double>

 

The WSDL file generated by the system to define this server is viewable here.

 

Note in the portType element there are two operations specified whose names are the method names in the C# web service

  <portType name="ExampleSoap">

    <operation name="HelloWorld">

      <input message="s0:HelloWorldSoapIn" />

      <output message="s0:HelloWorldSoapOut" />

    </operation>

    <operation name="toThe">

      <input message="s0:toTheSoapIn" />

      <output message="s0:toTheSoapOut" />

    </operation>

  </portType>

 

The messages specified by the toThe operation point back to message element

 

  <message name="toTheSoapIn">

    <part name="parameters" element="s0:toThe" />

  </message>

  <message name="toTheSoapOut">

    <part name="parameters" element="s0:toTheResponse" />

  </message>

 

The element attributes point to type elements that define the data

 

      <s:element name="toThe">

        <s:complexType>

          <s:sequence>

            <s:element minOccurs="1" maxOccurs="1" name="number" type="s:float" />

            <s:element minOccurs="1" maxOccurs="1" name="n" type="s:int" />

          </s:sequence>

        </s:complexType>

      </s:element>

      <s:element name="toTheResponse">

        <s:complexType>

          <s:sequence>

            <s:element minOccurs="1" maxOccurs="1" name="toTheResult" type="s:double" />

          </s:sequence>

        </s:complexType>

      </s:element>