Protocols, Web Services and .NET

powered by FreeFind
Modified: 

Overview

Some portions of the following Overview have been excerpted from Microsoft XML Web Service Wire Formats

The Internet and Web's value is largely defined by the sharing of resources.

.NET Overview

.NET is built upon several standard protocols to implement RPC (Remote Procedure Call) that is computer architecture AND language neutral. The implementation of .NET uses the following protocol hierarchy:

The use of standard protocols (note that IIS could be any Web server capable of invoking Web services) allow RPC client and Web services to be implemented in a wide variety of approaches, a SOAP knowledgeable Web browser could act as a client (consumer) of a Web service written in C++, Visual Basic, etc.

The list of protocols and a rough hierarchy within .NET is:

  1. .NET Programming language (e.g. C#) - Implements algorithms on client and server utilizing RPC. 
  2. .NET - Defines the infrastructure used by languages to implement more or less transparent RPC. 
  3. SOAP - Simple Object Access Protocol, a subset of XML that defines a common representation for basic data types such as integer, character, etc. and objects 
  4. XML - eXtensible Markup Language to define and represent arbitrary data structures in SOAP.
  5. HTTP - Protocol to pass XML messages between client and server.
  6. IIS - Internet Information Services - Handles communication between client and server.
  7. TCP - Reliable point-to-point communications over a network.
  8. IP - Internet Protocol.
  9. Physical layer - Ethernet/Point-to-Point/etc.

RPC

One high-level approach to distributed system implementation follows the standard model of calling a function, the difference is that the client caller and the server callee function are on different computers.

The steps required to execute the RPC for sqroot:

Client Program Client Proxy      Transmit   Server
sqroot(16.0) call to proxy.      
Block waiting for result. Parameter 16.0 translated by the proxy to a common number representation.  
Block waiting for result. Open connection to server. Parameter 16.0 and call to sqroot are passed as message over the network to server sqroot
16.0
Receive message of sqroot function and parameter 16.0
Block waiting for result.     Determine sqroot is destination function to call and translate 16.0 from common number representation to machine representation
Block waiting for result.     Execute sqroot for result 4.0
Block waiting for result.     Translate result 4.0 into common number representation
Block waiting for result. Receive message of 4.0 4.0 Result 4.0 is passed as message over network to client
Block waiting for result. Translate result 4.0 from common number representation to internal representation. Return result 4.0 to client program.  
cout << 4.0      

StockQuote RPC

A real example of a client side RPC, this to a foreign machine, is a delayed stock quote provided through the http://www.xmethods.net server. The following displays the quote for IBM.

StockQuote.vbs

  Set gr = CreateObject("MSSOAP.SoapClient") 
  gr.mssoapinit "http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl"
  wscript.echo gr.getQuote ("IBM")

IIS

.NET or other web service infrastructure requires a contact point on the server machine, IIS for Microsoft systems.

HTTP-GET and HTTP-POST

Client - A computer that accesses shared network resources provided by another computer, called a server. An Internet client application is a program that accesses information from a network data source (server) using Internet protocols such as gopher, FTP, or HTTP. An Internet client application might access a server to retrieve data such as weather maps, stock prices, or newspaper headlines, for example. The Internet client can access the server through an external network (the Internet) or an internal network (sometimes called an intranet).

Web browsers are a common Internet client, providing a graphical interface to resources stored on a Web server. The relationship of a client and server is illustrated by the following diagram. The communication exchange is as follows:

  • Client connects to a server at www.ius.edu.
  • Client sends a request to server as rwisman/Hello.htm
  • Server receives request, retrieves the file Hello.htm and copies the contents of the file back to the client.
  • Client receives server response as the contents of file Hello.htm and renders the contents as Hello World.

Server - In general, refers to a computer that provides shared resources to network users. On a network, the computer running software that provides data and services to clients over the network.  On the Internet, refers to a network data source using Internet protocols such as gopher, FTP, or HTTP. In the illustration above, the server provides file resources using the HTTP protocol.

HTTP - The rules for Web browser and server interaction are defined by the HyperText Transfer Protocol (HTTP). HTTP supports four basic operations that the browser can specify:

  • GET requests a specific item from the server such as a file. The server returns a status heading, a blank line, followed by the item.
  • HEAD requests item status, the server returns only the status.
  • POST sends data to the server.
  • PUT sends data to the server.

The communication exchange listed above would be more accurately specified as an HTTP exchange.

GET /rwisman/Hello.htm HTTP/1.0
HTTP/1.1 200 OK
Server: Microsoft-IIS/4.0
Date: Tue, 10 Apr 2006 03:37:48 GMT
Content-Type: text/html
Accept-Ranges: bytes
Last-Modified: Tue, 10 Apr 2006 03:35:48 GMT
ETag: "021f556fc1c01:60ca"
Content-Length: 24

<H1>Hello World</H1>

XML common language 

For communicating arbitrary data structures or objects both the client and server must have a common representation in XML. Consider the C++ and corresponding XML:
 

C++ MyObject obj instantation XML representation of MyObject obj instantation
class MyObject {
  public int n1;
  public int n2;
  public String str;

MyObject obj = new MyObject();
obj.n1 = 1;
obj.n2 = 24;
obj.str = "Some String";

server.f(obj);

    <a1:MyObject id="ref-1">
      <n1>1</n1>
      <n2>24</n2>
      <str id="ref-3">Some String</str>
    </a1:MyObject>
 
 
 
 
 
 

 

SOAP

SOAP is a simple, lightweight XML-based protocol for exchanging structure and type information on the Web.

The overall design goal of SOAP is to keep it as simple as possible, and to provide a minimum of functionality. The protocol defines a messaging framework that contains no application or transport semantics. As a result, the protocol is modular and very extensible.

By traveling over standard transport protocols, SOAP is able to leverage the existing open architecture of the Internet and gain easy acceptance by any arbitrary system capable of supporting the most basic Internet standards. You could view the infrastructure required to support a SOAP-compliant XML Web service as rather simplistic, yet powerful, since it adds relatively little to the existing infrastructure of the Internet and still facilitates universal access to the services built with SOAP.

The SOAP protocol specification consists of four main parts.

  1. The first part defines a mandatory extensible envelope for encapsulating data.
  2. The second part of the SOAP protocol specification defines optional data encoding rules for representing application-defined data types and directed graphs, and a uniform model for serializing non-syntactic data models.
     
  3. The third part defines an RPC-style (request/response) message exchange pattern. Each SOAP message is a one-way transmission.
  4. The fourth part of the specification defines a binding between SOAP and HTTP.

Web Services

Web services are essentially remote procedure calls from a client that are executed on a server. The two key elements of Microsoft .NET are:

  1. support for Web services implementation through the Visual Studio, VB Script, etc.
  2. response to service requests through the IIS server.

Example

Following contacts a current temperature for a zip code Web service, the client is written in VBScript. The three lines of code are:

  1. Set gr = CreateObject("MSSOAP.SoapClient") - create a Soap client object.
  2. gr.mssoapinit "http://www.xmethods.net/sd/2001/TemperatureService.wsdl" - Contact service, receiving Web Services Description Language of how to call the service.
  3. gr.getTemp ("47150") - Call the service with parameter "47150" and receive service result.

The script can be executed at the command line by (assuming named tempquote.vbs):

cscript tempquote.vbs

Set gr = CreateObject("MSSOAP.SoapClient")
gr.mssoapinit "http://www.xmethods.net/sd/2001/TemperatureService.wsdl"
wscript.echo gr.getTemp("47150")

Example:

A Web service, MathService, written in C#, defines two operations (Plus and Minus) and can be invoked through HTTP GET, HTTP POST, or SOAP protocols.

  1. MathWebService/bin/MathWebService.dll - Is the executeable code which implements the service. MathService.asmx.cs is the C# program.
  2. MathWebService/MathService.asmx - Holds the service description and location needed by the IIS server to pass parameters and execute the requested service methods.

requests MathService.asmx which executes the code from the MathService.asmx.cs program that implements the Web service. 

The code is in bin/MathWebService.dll file, which is executed through the IIS server.

MathService.asmx (Server)

<%@ WebService Language="c#" Codebehind="MathService.asmx.cs" Class="MathWebService.MathService" %>

 

MathService.asmx.cs (Server)

using System.Web.Services;

namespace MathWebService   {
      [WebService(Namespace="http://localhost/MathWebService/")]
      public class MathService : System.Web.Services.WebService
      {
            public MathService()
            {
                  InitializeComponent();
            }
            [WebMethod]
            public int Plus(int x, int y
            {
                  return x+y;
            }
            [WebMethod]
            public int Minus(int x, int y)
            {
                  return x-y;
            }
            private void InitializeComponent(){}
     
}
}

Implementing: It is simpler to use .NET Visual Studio to implement Web services in any of the supported languages (C#, VB, C++). Much of the administration (such as creating an IIS virtual directory, compiling and uploading files, etc.) are performed automatically. Essentially all that is necessary is to create a C# project as a Web Service and edit in the service methods.

Implementing MathService in Visual Studio

Do the following steps:

  • Visual Studio .NET | File | New | Project | Visual C# Projects | ASP .NET Web Service
  • Location: http://localhost/MathWebService

This creates a MathWebService project and virtual directory and skeleton C# program named Service1. Click "switch to code view" for the program code.

  • View | Solution Explorer
  • Right click on Service1.asmx
  • Rename to MathService.asmx
  • Copy and paste the above program MathService.asmx.cs.
  • Build | Build MathWebService

Click to execute the service:

Browser as a Client

Browser's speak HTTP protocol so can act as a simple client, below invoking MathService.Plus(4,6); via HTTP GET protocol. 

Browser as a Client

Change the following to use the Minus method.

VBScript as a Client

VB scripts can act as a client also, contacting servers for information. The following VB script MathClient.vbs uses the MathService Plus and Minus methods.

MathClient.vbs

   set SOAPClient = createobject("MSSOAP.SOAPClient")
   SOAPClient.mssoapinit("http://localhost/MathWebService/MathService.asmx?WSDL") 
   wscript.echo SOAPClient.Plus (6,4) + SOAPClient.Minus(3,2)

Points of note:

  1. var SOAPClient = Server.CreateObject("MSSOAP.SoapClient"); - Create a SOAP object.
  2. SOAPClient.mssoapinit ("http://localhost/MathWebService/MathService.asmx?WSDL");  - Initializes the SOAP object from the WSDL (Web Services Description Language) tree returned that defines how to call the Plus and Add methods. Click the link for the WSDL of the service.
  3. wscript.echo SOAPClient.Plus (6,4) + SOAPClient.Minus(3,2) - Call the methods and write result.

WSDL (Web Services Discovery Language)

A part of the WSDL returned by the server for the MathService is listed below. Telnet was used to request the WSDL to demonstrate that HTTP is the underlying protocol used (in this case).

User input
telnet localhost 80
GET /MathWebService/MathService.asmx?WSDL HTTP/1.0
MathService

namespace MathWebService   { 
  [WebService(Namespace="http://localhost/MathWebService/")] 
  public class MathService : System.Web.Services.WebService 
  { 
            public MathService() 
            { 
                  InitializeComponent(); 
            } 
            [WebMethod] 
            public int Plus(int x, int y) 
            {
                  return x+y; 
            } 
} 
Partial WSDL for MathService

HTTP/1.1 200 OK
Server: Microsoft-IIS/5.1
Date: Sat, 14 Jun 2003 13:53:50 GMT
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Content-Length: 6531

<?xml version="1.0" encoding="utf-8"?>
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
targetNamespace="http://localhost/MathWebService/"
xmlns="http://schemas.xmlsoap.org/wsdl/">
  <types>
    <s:schema elementFormDefault="qualified" targetNamespace="http://localhost/MathWebService/">
      <s:element name="Plus">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="1" maxOccurs="1" name="x" type="s:int" />
            <s:element minOccurs="1" maxOccurs="1" name="y" type="s:int" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="PlusResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="1" maxOccurs="1" name="PlusResult" type="s:int" />
          </s:sequence>
        </s:complexType>
      </s:element>
     <service name="MathService">
      <port name="MathServiceSoap" binding="s0:MathServiceSoap">
        <soap:address location="http://127.0.0.1/MathWebService/MathService.asmx"/>
      </port>
    </service>
</definitions>

Windows Client

The MathService can be invoked by a client program in Windows as illustrated below:

The client side consists of two programs: 

MathService ms = new MathService();
Console.WriteLine( ms.Plus(6, 8) );

invokes the proxy Plus(6,8) method which in turn invokes the MathService Plus(6,8) method on the server.

MathClient.cs (Client)

namespace MathServiceConsumer {
            using System;
            using MathProxy;
            public class MathClient {
                        public static int Main(string[] args) {
                                    MathService ms = new MathService();
                                    Console.WriteLine( ms.Plus(6, 8) );
                                    return 0;
                        }
            }
}

this.Url = "http://localhost/MathWebService/MathService.asmx"; 

is the location of the MathService. The proxy defines an interface to the Plus method as:

       public int Plus(int x, int y) {
            object[] results = this.Invoke("Plus", new object[] {  x, y });
            return ((int)(results[0]));
        }

The proxy Plus method is invoked by the client identically to the service Plus method, however the proxy Plus invokes the service Plus method located on a server.

MathServiceProxy.cs (Client)

  namespace MathProxy {
           ….
    using System.Web.Services;

    Namespace="http://localhost/MathWebService/")]
    public class MathService : System.Web.Services.Protocols.SoapHttpClientProtocol {
        public MathService() {
            this.Url = "http://localhost/MathWebService/MathService.asmx";
       }
         ...
       public int Plus(int x, int y) {
            object[] results = this.Invoke("Plus", new object[] {  x, y });
            return ((int)(results[0]));
        }
         ...
    }
}


Implementing: The client can be implemented within or without the .NET Visual Studio. The browser request:

http://localhost/MathWebService/MathService.asmx?WSDL

produces the formal specification of the MathService in SOAP.

Windows Client

The command-line tool, WSDL, will generate the corresponding C# directly from the SOAP specification. The following generates MathServiceProxy.cs with the namespace MathProxy and compiles MathServiceProxy.cs and MathClient.cs:

  1. Note that the MathWebService virtual drive must be defined and contain the MathService.asmx program.
  2. Copy and paste MathClient.cs
  3. Open a Command prompt window, change to a directory for creating MathClient.cs.
  4. Copy and paste the following commands in the Command prompt window to generate the MathServiceProxy.cs and compile with the MathClient.cs program.
    • PATH=%PATH%;C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Bin;

      PATH=%PATH%;C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705;

      wsdl /out:MathServiceProxy.cs http://localhost/MathWebService/MathService.asmx?WSDL /n:MathProxy

      csc /r:system.web.services.dll /r:System.xml.dll /t:library /out:MathServiceProxy.dll MathServiceProxy.cs

      csc /r:system.web.services.dll /r:MathServiceProxy.dll /t:exe MathClient.cs
  5. In Command prompt window, enter the following to run the MathClient:
    • MathClient

Use: The Windows executable file, MathClient.exe, can then be executed at the command-line, producing the result of 14.

C:> MathClient

14