Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

Subject : Dot Net Technology (Department Elective)

Semester : 6th - Branch : CE - Subject Code : 2160711

Lecture notes
Chapter-11

1. Demonstrate web services with any example?

- Web services are program components that allow you to build distributed, platform
independent application.
- These applications use standard protocol such as HTTP, XML, SOAP and WSDL to
provide various important services.
- Web services uses xml based messaging to send & receive data.
- You can use xml web service to integrate application that are written in different
programming language and also deployed on different platforms.
- In addition you can deploy XML web services within an Intranet as well as Internet.
- One important feature of web services is that a client need not to know the language
in which web service are implemented. The client just need to know the location of
web services and the methods that client can call.
- In web service model both the client & XML Web service are unaware of the
implementation details of each other.
- Now example in which you can implement web services. (calculation of income tax
that paid by customer)
- Web service that computes income tax requires a client application to provide
information such as income, savings and investments.
- A client application can call a method on services and provide necessary information
as arguments in method call. Now data related to method call and arguments is sent
to the web services in XML format using the SOAP protocol over the HTTP transport.
- Also you can find various free web services like sendsmstoindia, sendsmstoword,
currencyconverter etc…
- Also you can implement that kind of web services with your own logic.

2. List Steps to Create and Consume Webservice.

[6th CE – DOT NET TECHNOLOGY] Page 1


ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

Creating the Web Sevice:

- A web service is an web application which is basically a class consisting of methods


that could be used by other applications.
- To understand the concept let us create a web service that will calculate addition,
subtraction, multiplication of two inputs.
- The clients will pass two values from webpage and web service will do calculation
and provide correct result.
- This web service will have three methods:

 Default HelloWorld method


 addition Method
 multiplication Method

Take the following steps to create the web service:

Step (1): Select File--> New --> Web Site in Visual Studio, and then select ASP.Net Web
Service.

Step (2): A web service file called Service.asmx and its code behind file, Service.cs is
created in the App_Code directory of the project.

Step (3): The .asmx file has simply a WebService directive on it:

<%@ WebService Language="C#" CodeBehind="~/App_Code/Service.cs"


Class="Service" %>

Step (4): Open the Service.cs file. Default web service code behind file looks like the
following:

using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo =
WsiProfiles.BasicProfile1_1)]

public class Service : System.Web.Services.WebService

[6th CE – DOT NET TECHNOLOGY] Page 2


ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

{
public Service () {

[WebMethod]
public string HelloWorld() {
return "Hello World";
}
}

Step (5): Change the code behind file to add two web methods for getting the addition
and subtraction of two values as shown below:

using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo =
WsiProfiles.BasicProfile1_1)]

public class Service : System.Web.Services.WebService


{
public Service () {

[WebMethod]
public string HelloWorld()
{
return "Hello World";
}

[WebMethod]
public int addition(int a, int b)
{
return a + b ;
}

[WebMethod]

[6th CE – DOT NET TECHNOLOGY] Page 3


ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

public int multiplicaton(int a, int b)


{
return a * b;
}
}

Step (6): Running the web service application gives a web service test page, which allows
testing the service methods.

Step (7) : Click on a method name, and check whether it runs properly.

[6th CE – DOT NET TECHNOLOGY] Page 4


ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

Consuming the Web Service:

- After a creating a web service, the client can access the services provided by it. This is
known as consuming web services.
- Any applications that have proper permission to access web services can access your
XML web service and consume it services.
- The application that consumes web service is known as web service client.
- To access web service from client application you need to perform following steps.

(1) Add web reference to the web services in the client application.

- To use web services created by other programmer you should know the location of
the web service. Also you can search it via UDDI.
- To discover web service easily, visual studio.net provide a Web Reference for each
xml web service.
- You can Add Web Reference to your project by using Add Web Reference dialog box.
The Add Web Reference dialog box uses the discovery mechanism to locate xml web
services.
- Add Web Reference dialog box requests the URL for web service on site and display
it.

[6th CE – DOT NET TECHNOLOGY] Page 5


ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

- After select web service, you click Add Reference button to Add Web Reference in
your project

(2) Generate proxy class for the XML web services.

- When you click the Add Reference button, visual studio download service description
to the local computer and generate a proxy class for the web service.
- The proxy class of web service contains information for calling each web service
method.
- Visual studio uses WSDL (Web service Description Language) to create proxy class.
- The proxy class is described in the .wsdl file.
- You can use wsdl.exe to generate proxy class manually for the web services.

(3) Create an object of the proxy class in the client application.

- After you Add Web Reference and generate proxy class, create an object of the proxy
class in the client application.
- The object of the proxy class allows you to invoke the methods of the web service
and access the result o it.

(4) Access the xml web services by using a proxy object.

- After you create an object of the proxy class, you can easily write code against an xml
web service.
- By this proxy object you can access all methods of web service.
- For Example, let us consuming above web service for addition and multiplication of
two values.

protected void Button1_Click(object sender, EventArgs e)


{
Service obj = new Service();

[6th CE – DOT NET TECHNOLOGY] Page 6


ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

TextBox3.Text =
obj.addition(Int32.Parse(TextBox1.Text),
Int32.Parse(TextBox2.Text)).ToString();
}

protected void Button2_Click(object sender, EventArgs e)


{
Service obj = new Service();
TextBox3.Text =
obj.multiplicaton(Int32.Parse(TextBox1.Text),
Int32.Parse(TextBox2.Text)).ToString();
}

3. Explain SOAP and WSDL?

WSDL: (Web Service Description Language)

- WSDL is the web service description language that specifies how client interact with
a web service including details like which methods are present in web service, which
parameters and return value each method use, which protocol should be used for
transmission over internet.
- Currently three standards are supported for transmission of web service information:
HTTP GET, HTTP POST and SOAP.
- Asp.net creates WSDL documents for your web services automatically.
- Asp.net can also create a proxy class based on the WSDL document automatically.
- WSDL document contains information for communication between a web service
and client.
- It does not contain any information regarding coding or implementation of your web
service method.

SOAP: (Simple Object Access Protocol )

- SOAP is the default for Asp.net that automatically generates proxy class.
- SOAP works over HTTP but uses special xml-like format for bundling information.
- SOAP enables you to consume complex data structure like dataset or just table of
data.
- SOAP relatively simple and easy to understand.
- Asp.net web service generally uses soap over http using the http post protocol.
- An example of SOAP request (from client to web service) shown below.

[6th CE – DOT NET TECHNOLOGY] Page 7


ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

SOAP request:

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Body xmlns:m="http://www.example.org/stock">
<m:GetStockPrice>
<m:StockName>IBM</m:StockName>
</m:GetStockPrice>
</soap:Body>

</soap: Envelope>

SOAP response:

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Body xmlns:m="http://www.example.org/stock">
<m:GetStockPriceResponse>
<m:Price>34.5</m:Price>
</m:GetStockPriceResponse>
</soap:Body>

</soap:Envelope>

- You can see that the root element is <soap:envelope> which contains the
<soap:body> of the request.
- Inside the body, web method GetStockPrice is being called.
- Now, In response to this request, SOAP output message will be returned.

4. Describe the Process of Webservice communication.

The WSDL and SOAP standards enable you to communicate between client & web
services but they do not show how this communication happens.
- The following three components play role for this:
(1) A custom web services class: That provides some piece of functionality.
(2) A client application: That wants to use this functionality.

[6th CE – DOT NET TECHNOLOGY] Page 8


ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

(3) A proxy class: That acts as a interface between client application & web service class.
A proxy class represents all the web service methods and takes care of communication
with web service by chosen protocol.

Actual process work like this:


1. The client creates an instance of proxy class.
2. The client invoke the method on proxy class, exactly same as normal local class.
3. Now proxy class sends the information to the web server in appropriate format
(usually SOAP) and receives the corresponding response.
4. The proxy class returns the results to the calling code.

- In this scenario, client does not need to aware regarding remote function call made to
web service. You have to just call function in own local code.
- Some limitation occurs during this process like, Not all data types are supported for
method parameters and return value.
- For example you cannot pass many .net class library objects (The dataset is one
exception)
- If error occurs that can interrupt your web method like network problems.

5. Discovering Webservice & UDDI.

- Once you create Webservice, how can client find your Webservice this is the first basic
questions.

[6th CE – DOT NET TECHNOLOGY] Page 9


ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

- Clearly, Webservice is available at specific URL address. Once client type this URL
address then all the necessary information is available. So why discovering standard
required?
- Now process described above works great, if you only need to serve a Webservice to
specific clients or inside a single organization.
- If company provides dozens of webservices, how can it communicate with each other to
prospective clients?
- Individually serve Webservice taking time and create inefficiency.

DISCO standard

- When use Disco standard you provide a .disco file that specifies where a webservices is
located.

- Asp.net tools such as visual studio.net can read the discover file and automatically
provide you list of corresponding web services.

- The benefit of a .disco file is that it is clearly used for Webservice (while .html and .aspx
file can contain any kind of content)

- When visual studio.net creates a discovery file, it uses the extension .vsdisco

UDDI (Universal Description Discovery and Integration)

- UDDI is youngest and most rapidly developing standard.

- Using UDDI it is easy for you to locate Webservice on any server.

- It is business registry that lists information about companies.

- The goal of UDDI is to provide collection where business can advertise all the Webservice
they have.

- For example, a company has list of services for business document exchange so you
must have to register it with UDDI service.

- There are three main sections of UDDI

1. White pages, which provide business contact information

2. Yellow pages, which organize xml Webservices into categories

[6th CE – DOT NET TECHNOLOGY] Page 10


ADITYA SILVER OAK INSTITUTE OF TECHNOLOGY (CE/IT DEPARTMENT)

3. Green pages, which provides detailed information about individual services.

- UDDI registry defines complete programming interface that specifies how SOAP message
can be used to retrieve information about business object.

- In other word, UDDI registry is itself a Webservice.

- This standard is still too new to really use, but we can find detailed information at
http://www.uddi.microsoft.com

[6th CE – DOT NET TECHNOLOGY] Page 11

You might also like