Consuming a web service : A real time example

This article explains you how to consume a live web service in an ASP.NET web applicationwith screenshots and required code snippets.  

 
Here is the URL of a live weather forecast web service.

Live Weather

We can see there are three methods available in this Weather forecast web service above. They are:
  1. GetCityForecastByZIP
  2. GetCityWeatherByZip
  3. GetWeatherInformation.

If you click the “Service Description” link, you'll be taken to the Web Service Description Language (WSDL). Visual Studio uses this WSDL to generate proxy classes, so that the client can use those proxy classes to invoke the web methods of the web service.

Here, I will use the “GetCityWeatherByZip” method. Click on this method, you'll be able to see the following screen.

Provide any valid Zip Code (only a United States Zip Code), I'm giving here “64131” and clicking the invoke method to get the weather information in that area Zip code.

Weather

Here I entered 64131 (Kansas City area) Zip code, so I'm getting weather information in that area in XML format.

XML

We can notice the following elements in the preceding XML file.

  1. <Success>true</Success>        (true means the zip code we’ve given is valid)  
  2. <ResponseText>City Found</ResponseText>   (City Found means It’s a valid zip otherwise It returns “Invalid Zip Code” message)  
  3. <State>MO</State>  
  4. <City>Kansas City</City>  
  5. <WeatherStationCity>Lees Summit</WeatherStationCity>  
  6. <WeatherID>10</WeatherID>  
  7. <Description>Mostly Sunny</Description>  
  8. <Temperature>N/A</Temperature>  
  9. <RelativeHumidity>N/A</RelativeHumidity>  
  10. <Wind>MISG</Wind>  
  11. <Pressure>29.93F</Pressure> 

Now let's create an ASP.NET application to consume the weather forecast web service in real time.

Step 1: Create an ASP.NET Empty Web Application.

Web Application

Step 2: Add Service reference.

After creating ASP.NET Web Application, our next step is to add a service reference to it. This can be done by right-clicking on References then selecting Add service reference then in the Address field enter the URL of the weather service then click “Go”. You will then see the Weather service then expand the Weather service then click WeatherSoap.

It will show you all the methods of the Weather Web Service. Finally provide a meaningful name in the Namespace field and click “OK”. Please refer to the following screenshot for a better understanding.

Namespace

After adding the service reference, it will generate proxy classes. Have a look at the following screen shot to determine the proxy classes in the project.

proxy classes

Here the Refrence class is the auto-generated proxy class.

Step 3: Create a Web From.

Now let's create a Web Form with some controls that looks as in this.

Create a Web From

I want to display the City, State, Weather Station City, Temperature and Wind Direction in the web form when the user enters a valid zip code.

(Note: If you want to display more properties, please refer to the XML file as in Figure 3.)

Now I will create a Web form. You can find the procedure in the following screenshot.

Right-click on the project then seelct Add->New Item then select Web form then provide a name then click ”OK”.

Web from

Create your web form with some labels, TextBox and a button as shown below.

textbox

Now click the button in the web form to generate a click event handler. It will generate a button click event, you will see that in the following screen.

event handler

Write the following code in the button click event. (Note that in the following code you'll find “WeatherService” is the namespace I've given when adding the service reference, you can give your own namespace. Refer to Figure 5.)

And also ensure your label, button and TextBox properties are correct. Don't use mine.
label

Don't worry about the code above. It is easy to understand. In the first line I created an object for WeatherSoapClient that invokes WeatherService.

  1. WeatherService.WeatherSoapClient client=new WeatherService.WeatherSoapClient("WeatherSoap");

In line 2, I am invoking the GetCityWeatherByZIP method (because we are trying to get the weather information using a Zip code) and ing a Zip code as an argument.

  1. WeatherService.WeatherReturn result= client.GetCityWeatherByZIP(txtZip.Text);  

Inside the “if” I am validating the Zip code to ensure that the Zip Code is correct, in other words result.success is true. Then all the details should be displayed in the web form.

For example, here result.City displays the name of the City based on the Zip Code.

If Zip Code is incorrect, in other words result.success is false, then control goes to else part and displays only an error message.

  • result.ResponseText gives Invalid Zip Code (for more clarity, refer to the XML file Figure).

  • string.Empty means it clears all the label values (displays nothing).

Step 4: Run the application

Press Ctrl+F5 to run the application. The enter any valid USA Zip code. I entered 22041.

Run the Application
If the Zip Code is wrong tehn:

ZIP Code

Thank you for reading, please leave a comment if you have any doubts or didn't understand.