Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 7 - Real time example of calling live weather forecast web service

Suggested Videos
Part 4 - WebMethod attribute properties
Part 5 - WebMethod overloading in asp.net web services
Part 6 - Calling asp.net web service from javascript using ajax



In this video we will discuss calling a LIVE weather forecast web service. The following is the URL of the real live web service. At the moment, this web service can only be used with US zip codes. There are 3 web methods in this web service. We will use GetCityWeatherByZIP method.
http://wsf.cdyne.com/WeatherWS/Weather.asmx



This web service can be used, if you want to display specific US city weather on your web site. The user interface of the application that we are going to develop, will be as shown below. Once we enter a ZIP code and when we click Get Weather button, we want to invoke the weather web service and display weather information of the provided zip code.
Real time example of calling live weather forecast web service

Here are the steps to consume this web service in an asp.net web application
Step 1: Create a new asp.net empty web application and name it WebServicesDemo.

Step 2: Right click on References folder in the WebServicesDemo project and select Add Service Reference option.

Step 3: In the Address textbox of the Add Service Reference window, type the below web service address and click GO button. In the namespace textbox type WeatherService and click OK.
http://wsf.cdyne.com/WeatherWS/Weather.asmx

Step 4: Right click on WebServicesDemo project in solution explorer and add new webform.

Step 5: Copy and past the following HTML on WebForm1.aspx
<table style="font-family:Arial; border:1px solid black">
    <tr>
        <td>
            <b>Zip Code </b>
        </td>
        <td>
            <asp:TextBox ID="txtZip" runat="server"></asp:TextBox>
            <asp:Button ID="btnGetWeather" runat="server" Text="Get Weather" 
                onclick="btnGetWeather_Click"/>
        </td>
    </tr>
    <tr>
        <td>
            <b>City :</b>
        </td>
        <td>
            <asp:Label ID="lblCity" runat="server"></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <b>State :</b>
        </td>
        <td>
            <asp:Label ID="lblState" runat="server"></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <b>WeatherStationCity :</b>
        </td>
        <td>
            <asp:Label ID="lblWeatherStationCity" runat="server"></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <b>Temperature :</b>
        </td>
        <td>
            <asp:Label ID="lblTemperature" runat="server"></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <b>Wind :</b>
        </td>
        <td>
            <asp:Label ID="lblWind" runat="server"></asp:Label>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <asp:Label ID="lblError" runat="server" Font-Bold="true" ForeColor="Red"></asp:Label>
        </td>
    </tr>
</table>

Step 6: Copy and past the following code on WebForm1.aspx.cs
using System;
namespace WebServicesDemo
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void btnGetWeather_Click(object sender, EventArgs e)
        {
            WeatherService.WeatherSoapClient client = new WeatherService.WeatherSoapClient("WeatherSoap");
            WeatherService.WeatherReturn result = client.GetCityWeatherByZIP(txtZip.Text);

            if (result.Success)
            {
                lblError.Text = "";
                lblCity.Text = result.City;
                lblState.Text = result.State;
                lblTemperature.Text = result.Temperature;
                lblWind.Text = result.Wind;
                lblWeatherStationCity.Text = result.WeatherStationCity;
            }
            else
            {
                lblError.Text = result.ResponseText;
                lblCity.Text = "";
                lblState.Text = "";
                lblTemperature.Text = "";
                lblWind.Text = "";
                lblWeatherStationCity.Text = "";
            }
        }
    }
}

asp.net webservices tutorial

6 comments:

  1. 1. How do you Get Credentials through IIS and Windows Authentication, Forms Authentication in Asp.net Web Application?
    2. An Asp.net Web Application Can Run Without Web.Config File? If Yes How many Web.Config file has an Application?
    3. What is Code Behind File & Code Fie? Main Difference?
    4. How do you display records in Grid View at at time that records are middle from the database table has contains 10,000 records?
    5. Can you Display 10,000 records in Grid View Control At a Time? If Yes Then how?How will you apply Customize Page in Grid View?
    6. Can You Apply Web service on IIS? Difference Between IIS and Web Service?
    7. How do you display the format xxx-xxx-xxxx of telephone number in Text Box Control?
    8. What is The Difference between Website and Web Application?
    9. Can you write Expressions in Constructor?If Yes How?

    ReplyDelete
  2. Hello sir,
    Thank you for sharing live example.
    My question is how to call this web service through javascript using ajax?
    Or
    How can invoke this web service in javascript?

    ReplyDelete
  3. Hi Venkat,
    Can you please explain us now how can we consume the Weather webservice using javascript from client side.
    Note:- I have tried to call the weather webservice as explained in video - 6 but it is telling " Java script error - WebserviceClient is undifined " may be because of it is not detecting the name space and class from the proxy class.

    Can you please explain how to implement it.

    ReplyDelete
  4. Hii venkat,

    I am not able to add the 3 methods from thr url.visual studio is complaining about file does not have the permisssion.

    ReplyDelete
  5. Hii venkat, How are you ?

    I tried to consume weather service using ajax and javascript, but i am unable to succeed.i am stuck. Can you please help me on this.
    This is my code.

    function GetWeather() {
    debugger;
    var Zip = document.getElementById('txtZip').value;
    //WebServiceDemo.WeatherService.Weather.GetCityWeatherByZIP(Zip, GetWeatherSuccessCallback, GetWeatherFailureCallback);
    $.ajax({
    type: "GET",
    contentType: "application/json",
    url: "http://ws.cdyne.com/WeatherWS/GetCityWeatherByZIP",
    dataType: "text",
    data: JSON.stringify({
    ZIP: Zip
    }),
    success: function(response) {
    // Put the plain text in the PRE tag.
    $("#putResponse").text(response);
    },
    error: function(error) {
    // Log any error.
    console.log("ERROR:", error);
    }
    });
    }

    ERROR:
    Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://ws.cdyne.com/WeatherWS/GetCityWeatherByZIP?{%22ZIP%22:%2222041%22}. This can be fixed by moving the resource to the same domain or enabling CORS. GetCityWeatherByZIP
    "ERROR:" XMLHttpRequest { abort: .ajax/w.abort(), onreadystatechange: c</<.noop(), readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, responseURL: "", status: 0, statusText: "", responseType: "" } Default.aspx:30

    ReplyDelete
  6. i am getting below ERROR;

    Server was unable to process request. ---> Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.