Asynchronous web service access
To see how to access a web service asynchronously, we'll work through an example based on accessing the "USA Weather Forecast" web service provided by http://www.webservicex.net
. One of its methods takes a US ZIP code, and returns a weather forecast for that location.
Synchronous version
Before tackling the asynchronous version, we'll quickly look at the standard synchronous way of accessing this service. You'll find the code in the code bundle in the folder ThreadStarvation_WebService_Sync
.
1. Add the web service to your website. Right-click on the website and choose Add Web Reference. In the URL field, enter the URL of the web service:
http://www.webservicex.net/WeatherForecast.asmx?WSDL
Note the description of the web service and its namespace:
net.webservicex.www.
Click on the Add Reference button to add the web reference.
2. Add a
using
statement to your code behind file to include the namespace provided by the web service:using net.webservicex.www;
3. To...