Showing posts with label WebServices. Show all posts
Showing posts with label WebServices. Show all posts

Dec 11, 2012

Consuming a web service using ASP .NET

To know about basics of web service read this article.
To know about creating a web service using ASP .NET read this article.

Background:
In the previous article we have learned about creating a web service. Now in this article, we will learn how to consume a web service using asp .NET.
Real Time Example:
Nowadays mobile application (Mobile Apps) are used widely all the users. Take a scenario, we install the “Weather” gadget in our mobile and check the weather of a particular place.
Think where we are receiving this weather information? It is actually we are consuming the web services.
We will select the region/place to see the weather, a web service will be called and the weather info is displayed.
Hence, Web Services are hosted somewhere and via internet we are consuming the data through the web service.

Implementation:
1. Create an ASP .NET web application with a simple design as shown below:
2. Now, we have to add the web service reference to this Project (Here we use the web service created in the previous article). Click on the Projects in solution explorer -> Add web reference -> give the url of the web service
(Note: to access the web service in the client use the appropriate machine/domain name)
Now, you will see the following screen:
3. Click on Go and search the web service and then give the web reference name say “Consume Service” in our example. Then click Add Reference button.
4. Now, in the solution explorer you will see the Web Reference being added as shown below:
5. We can use the methods exposed by the web service in our code and test the result. Sample code is shown below:
public partial class _Default : System.Web.UI.Page 
{
   protected void Page_Load(object sender, EventArgs e)
   {
   }
    protected void btnToCelsius_Click(object sender, EventArgs e)
    {
        double x = Convert.ToDouble(txtValue.Text);
        if(txtValue.Text != string.Empty)
        {
            try
            {
                ConsumeService.Service service = new ConsumeService.Service();

                lblOutput.Text = service.ToCelsius(x).ToString();                
                
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
    protected void btnToFahreinheit_Click(object sender, EventArgs e)
    {
        double x = Convert.ToDouble(txtValue.Text);

        if (txtValue.Text != string.Empty)
        {
            try
            {
                ConsumeService.Service service = new ConsumeService.Service();

                lblOutput.Text = service.ToFahreinheit(x).ToString();

            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}

6. Build and run the code. Test the result by giving some inputs as shown below:

The whole source code can be downloaded from this link.

Hope the three articles about the web services explained about the basics of web services, creating and consuming the web services using ASP .NET in a simple way.
Please free to comment which help me to write more.

Creating Web Service using ASP .NET

To know about basics of web service read this article.

1.   Create an ASP .NET web service application using Microsoft Visual Studio.
2. Take a scenario that we have to convert Celsius to Fahrenheit and vice versa.
3.   Below is the code for our scenario:
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://ConversionWebService.org/Temp")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    public Service () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }
    [WebMethod(Description = "Convert Celsius to Fahreinheit")]
    public double ToFahreinheit(double C)
    {
        return C * (9.0/5.0) + 32;        
    }

    [WebMethod(Description = "Convert Fahreinheit to Celsius")]
    public double ToCelsius(double F)
    {
        return (F - 32) * (5.0/9.0);        
    }
   }
4. Build and run the web service, you will see the following output:
5.Now, we will test the web service by giving input values and invoke it.Below are the screen shots which will show the input given and the results:


                             
Hope this article, explains you about creating and running a simple web service using ASP .NET
Here is the link to download the full source code.

To know about consuming a web service using ASP .NET read this article.

Basics of Web Services

Introduction:
In this article, we aim at learning the basics of web services and understand how does web service works.
WebService:
1.   Web services are the easy way to create communications between different applications of different platform.
2.  Web Services extend these models a bit further to communicate with the Simple Object Access Protocol (SOAP) and Extensible Markup Language (XML) to eradicate the object-model-specific protocol barrier.

Components of Web Service:
All the standard Web Services works using following components
  1. SOAP (Simple Object Access Protocol)
  2. UDDI (Universal Description, Discovery and Integration)
  3. WSDL (Web Services Description Language)
SOAP (Simple Object Access Protocol):
SOAP is an XML-based protocol to let applications exchange information over HTTP. Simply, SOAP is a protocol for accessing a Web Service.
  1. It is a communication protocol and it is a format for sending messages.
  2. It is designed to communicate via Internet (W3C standard)
  3. It is platform and language independent
  4. SOAP allows you to get around firewalls
WSDL (WebServices Description language):
It is a W3C standard used to describe and locate the web service in XML language format.
UDDI (Universal Description, Discovery and Integration):
  1. It is a directory for storing information about web service
  2. It communicates via SOAP
  3. It is built on .NET platform
Why web service?
  1. Interoperability
  2. Usability
  3. Deployability
  4. Reusability
For more details on the advantages and disadvantages refer this MSDN article: http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/435f43a9-ee17-4700-8c9d-d9c3ba57b5ef

To know about creating a web service using ASP .NET read this article.
To know about consuming a web service using ASP .NET read this article.