Web Services

Web Services

Distributed Computing Architecture

Client Process Server Process

  1. Proxy is a client side representative of the actual object on server. It implements the same set of interfaces as the actual object implements.

  2. When the client requests for an instance of a remote object, the underlying services (Framework) creates the actual object in the server process and the proxy object in the client process, the client gets the reference to the local proxy object.

  3. Along with the actual object a “stub” object is created on server. The proxy and stub are connected with each other over a socket and communicates with each other using a common protocol.

  4. Whenever the client calls the method of the proxy, the implementation in Proxy serializes all the parameters of the method and transports them to Stub i.e. proxy marshals the request to stub.

  5. On Server, Stub deserializes the request and passes the references of the objects as parameters to the actual method in actual object.

  6. Server method returns data to Stub, which serializes the data and return to proxy, proxy deserializes and return the data to the client object.

SUN (Stanford University Network – RMI (Remote Method Invocation) – JRMP(Java RMI Protocol)

OMG (Object Management Group) – CORBA Common Object Request Broker Arch. -IIOP (Internet Interoperable Protocol

MS (Microsoft) – DCOM (Distributed Component Object Model) – DEC – Object RPC(Distributed Computing Environment)

Problem: Because the proxy and the stub are tightly coupled to each other they must use the common protocol for communication. As the protocols used by RMI, COBRA and DCOM are native protocols they lack Interoperability.

WebService

A WebService is a business object running on a Web Server and providing its functionality to any type of application / client which supports “SOAP” (Simple Object Access Protocol) protocol.

SOAP = XML + HTTP. SOAP is a specification by W3C. – Its an architectural neutral protocol.

In .Net WebSevices are managed by ASP.NET Framework. They are mostly used for developing B-B (Business to Business) services/application. Any kind of application which we can develop in MS.NET can be a client to WebService.

WSDL (Web Service Description Language):

It is an XML based document containing complete information of the webservice including methods and their parameters, the URL etc…

For every webservice developed, the corresponding WSDL document is distributed to the clients. The clients using a native utility program generates a proxy class from a WSDL document. This proxy class then can be used by client application for invoking the functionality / methods of the webservice without worrying about the existence of SOAP protocol.

Steps for Developing WebService:

1. File New Web Site ASP.NET Web Service and rename Service.asmx to DemoService.asmx

In DemoService.asmx:

<%@ WebService Language=”C#” CodeBehind=”~/App_Code/DemoService.cs” Class=”DemoService” %>

  • A webservice class autogenerated by studio has the attribute [WebService] and is also inherited from System.Web.Services.WebService class. BOTH ARE OPTIONAL REQUIREMENTS. i.e. any public class linked through file with extension ASMX and deployed in the webapplication can treated as a WebService.

  • All the methods of a webservice class to be used by the clients on internet must be marked as “WebMethod” ( attribute)

Step 2 [WebMethod]

public int Add(int a, int b)

{

return a + b;

}

  • Every SOAP request and response are made up of SOAPEnvelope. SOAPEnvelope has SOAPHeader, SOAPBody, SOAPFault.

  • SOAP always uses Http Post method and the SOAPEnvelope is submitted as MessageBody.

  • A Webservice developed in .Net can be used by an Http Client also. Here the method name is submitted as the PathInfo and the parameters are posted as a string of Name-Value pairs.

http:// ServerName/ApplicationName/ DemoService.asmx/ MethodName

SOAP Request

POST /DecWebServices/DemoService.asmx HTTP/1.1

Host: localhost

Content-Type: text/xml; charset=utf-8

Content-Length: length

SOAPAction: “http://tempuri.org/Add”

<?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>

<Add xmlns=”http://tempuri.org/”>

<a>int</a>

<b>int</b>

</Add>

</soap:Body>

</soap:Envelope>

SOAP Response

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>

<AddResponse xmlns=”http://tempuri.org/”>

<AddResult>int</AddResult>

</AddResponse>

</soap:Body>

</soap:Envelope>

Note: To view the WSDL document we can use the following URL

http://localhost/SepWebServices/Service.asmx?wsdl

Client Application: (In New Instance of VS.NET)

Step 3: File New Project Windows Application

Step 4: In the Solution Explorer References Right Click Add Service Reference In URL give the Url of Web Service(can copy from browser where we can see the webservice methods) Click on Go.

Step 5: In the same dialog: Namespace = localhost (name of domain on which the webservice is hosted is used) Click on Add Reference

Note: When a WebService reference is added in VS.Net that is using add Web reference it automatically creates a proxy class in the current project using a utility program called as WSDL.EXE. The same can be viewed by opening the file Reference.cs. (Before this click on Show All Files in Solution Explorer).

Step 6:

using WebServiceClientAppName.localhost

private void btnDemo_Click(object sender, EventArgs e)

{

DemoServiceSoapClient ds = new DemoServiceSoapClient();

MessageBox.Show(ds.Add(20, 50).ToString());

}

Step 7: Run the Client Application

Note: If a new method is added to the webservice class, right click on web reference of the webservice and select Update ervice Reference to reflect the new method in the proxy class.