Labels

Monday, July 7, 2008

04 - Consuming Web Services

Consuming XML Web Services:

After a client application discovers a Web service i.e once the proxy is created on the client application, the client application can access and use all the exposed methods of a Web service. The process of using an exposed method of a Web service is known as Consuming A Web Service.

· The application that consumes a Web service is known as the WEB SERVICE CLIENT.

· A Web service client can be a component, service, or desktop application. In fact, the most common Web service clients that consume Web services are Web applications and Web services.

Accessing the Web Service Using a Proxy Object

Below is the process used, when a client invokes methods on a Proxy class

Cleint à Call on Proxy à Serialize à Soap Message à Encrypt à Sent to Server

Server à Decrypt à Deserialize à Actual WebService Object à Execute the Method

Server à Call on Proxy à Serialize à Soap Message à Encrypt à Sent to Server

Client à Decrypt à Deserialize à Return value to ProxyObject -> ProxyObjec sends value to Client

· The client calls a method on the proxy object.

· The XML Web services infrastructure on the client system Serializes the method call and arguments into a SOAP message and sends it to the XML Web service over the network.

· The infrastructure on the server on which the XML Web service resides deserializes the SOAP message and creates an instance of the XML Web service.

· The infrastructure then calls the method with the arguments on the XML Web service.

· The XML Web service executes the method and returns the value with any out parameters to the infrastructure

· The infrastructure serializes the return value and any out parameters into a SOAP message and sends them to the client over the network.

· The infrastructure on the client computer deserializes the SOAP message containing the return value and any out parameters and sends them to the proxy object.

· The proxy object sends the return value and any out parameters to the client.

Important Facts:

· When we click the Add Reference button to add a reference, VS .NET adds a Web References node, which has the same name as the computer that provides the XML Web service, to your client application. You can rename the Web reference to change the namespace in which the proxy class is created. Or You can change the default localhost namespace by right-clicking localhost and clicking Rename on the shortcut menu

· When you click Add Reference, Visual Studio .NET includes a Web reference and adds the following files to your client application:

o .wsdl

o .disco

o .map

· The .wsdl file contains the proxy class generated by Visual Studio .NET. As described earlier, the proxy class contains instructions for calling XML Web service methods. In addition, the proxy class marshals arguments between an XML Web service and a client application. To view the .wsdl file that Visual Studio .NET creates, expand the Web References node under your project in the Solution Explorer.

· In addition, the Add Web Reference dialog box enables you to view the SOAP request and response, the HTTP-GET request and response, and the HTTP- POST request and response for an XML Web service method. The following code snippets display the SOAP request and response for an exposed method ‘HelloWorld()’of an XML Web service.

Note – For detail see ‘Anatomy of a SOAP Message’

* The SOAP protocol allows applications to exchange structured and typed information on the Web using XML-based standards.

* The data is exchanged in the form of SOAP Message.

* The data that is exchanged between the client application and the XML Web service follows a standard format - SOAP Message.

* The SOAP Message format consists of data that is encoded in an XML document. The XML document consists of a –

o Root Envelope element, which in turn consists of a mandatory Body Element.

o The Body element contains the data specific to the message.

o The Envelope element can contain an optional Header Element which contains additional information not directly related to the message. Each child element of the Header element is called a Soap Header.

POST /TrialService/Service.asmx HTTP/1.1

Host: localhost

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

Content-Length: length

SOAPAction: "http://tempuri.org/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://tempuri.org/" />

</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://tempuri.org/" />

</soap:Body>

</soap:Envelope>

Thanks & Regards,

Arun Manglick || Senior Tech Lead

No comments:

Post a Comment