Web Services in ASP.NET

What is a Web Service?
Web Services are a method of making information in way that available on web for any developer’s application.

Consuming Others Web services:
Calling third party web service is very easy just adding the web reference to particular web service to our application.

Here I am calling the web service to know the weather based on city and to get cities based on country. For this I am calling http://www.webservicex.net/globalweather.asmx web service.

To call web service got visual studio-> create New Site
->right click on Solution Explorer
-> click on Add Web Reference
-> in the URL text box , Enter web service URL which you want to import(here http://www.webservicex.net/globalweather.asmx) and press Go.
Then you can observe that web methods of particular services are displayed.
-> Then Press Add Reference (it is available at right side of the window)


Now you are successfully added the web service to your application.

Imports Namespace of that web service which includes all methods.
Here net.webservicex.www

To call http://www.webservicex.net/globalweather.asmx code look like below

 
Protected Sub btn1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn1.Click Dim obj As New GlobalWeather Response.Write(obj.GetCitiesByCountry(txt_country.Text)) End Sub Protected Sub btn2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn2.Click Dim obj As New GlobalWeather Response.Write(obj.GetWeather(txt_city.Text, txt_country.Text)) End Sub

There are methods here. First one returns list of cities based on Country name and second method returns the weather of particular city of a country.

Enter Japan in the Textbox and click on GetCitiesByCountry Button ,then you get the list of cities of Japan.

Enter Japan in the country textbox and Fuji Ab in city textbox, then click on GetWeatherByCity , you can get the weather report of particular city.

You can find source code at the end of this article.

Creating your web Service:

You can create your own web service. It is very easy to create your own web service with .Net Framework.

Open-> Visual studio-> File-> New web site
Right click on solution Explorer-> Select Add New Item
In the panel select Web service and Name it -> click Ok


In the web service code you can find sample function like below


<WebMethod()> _ Public Function HelloWorld() As String Return "Hello World" End Function

You can create youe own function with your own functionality.
Here I am creating simple function to returns the adding of two numbers. Adding function takes two numbers for input and it returns the sum two numbers as output.

The code look like below



<WebMethod()> _ Public Function Add(ByVal i As Integer, ByVal j As Integer) As Integer Return (i + j) End Function

You can test this web service easily.
Right click on Solution Explorer and Select Add Web Reference
And click on Web services in this solution .
Then it displays your web service, click on AddNumbers it displays methods of your web service. Click on Add Reference to add it to your application.

Import the namespace localhost.AddNumbers

Code to call web service


   
        Imports localhost.AddNumbers
        Partial Class _Default
            Inherits System.Web.UI.Page

            Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) 
                                Handles Me.Load
                Dim obj As New AddNumbers
                Response.Write(obj.HelloWorld & "<br/>")
                Response.Write("Result: " & obj.Add(10, 20))

            End Sub
        End Class

Download source code here