The key to understanding Web services is knowing something about the protocols that make them possible, which are as follows :
SOAPFor Web services to manipulate objects through XML messages, there has to be a way to translate objects (as well as their methods and properties) into XML. SOAP is a way to encapsulate method calls as XML sent via HTTP. Using SOAP to communicate with Web services has two major advantages. First, because HTTP is so pervasive, it can travel to any point on the Internet, regardless of intervening hardware or firewalls. Second, because SOAP is XML based, it can be interpreted by a wide variety of software on many operating systems. Disco and UDDIBefore you can use a Web service, you need to know where to find it. Protocols such as Disco and UDDI enable you to communicate with a Web server to discover the details of the Web services available at that server. WSDLAnother prerequisite for using a Web service is knowledge of the SOAP messages it can receive and send. You can obtain this knowledge by parsing WSDL files. WSDL is a standard by which a Web service can tell clients what messages it accepts and which results it will return. WSDL files define everything about the public interface of a Web service, including the following:
Invoking a Web ServiceNow that you know the basics, it's time to see a Web service in action. The following steps show how you can use a Web servicein this case, one that supplies the weather at any airport worldwide:
When you create the Web reference, for example, Visual Studio .NET reads the appropriate WSDL file to determine which classes and methods are available from the remote server. When you call a method on an object from that server, the .NET infrastructure translates your call and the results into SOAP messages and transmits them without any intervention on your part. When you invoke a Web service, by default the client uses synchronous methods to communicate with the Web service. This means that the client waits for the SOAP response before allowing any other code to execute. This can result in slow performance of the application. To increase the responsiveness of the client program, you can call the Web service asynchronously, as shown in the following code segment: private void btnGetWeather_Click(object sender, System.EventArgs e) { // Declare the Web service main object com.capescience.live.AirportWeather aw = new com.capescience.live.AirportWeather(); // Invoke the Web service asynchronously. // First, create a callback method AsyncCallback wcb = new AsyncCallback(WebServiceCallback); // And then initiate the asynchronous call IAsyncResult ar = aw.BegingetSummary(txtCode.Text, wcb, aw); // Process other code and then wait // for the asynchronous request to complete ar.AsyncWaitHandle.WaitOne(); } // This method will get called when the Web service call is done public void WebServiceCallback(IAsyncResult ar) { // Retrieve the state of the proxy object com.capescience.live.AirportWeather aw = (com.capescience.live.AirportWeather) ar.AsyncState; // Call the End method to finish processing com.capescience.live.WeatherSummary ws = aw.EndgetSummary(ar); // And display the results lbResults.Items.Clear(); lbResults.Items.Add(ws.location); lbResults.Items.Add("Wind " + ws.wind); lbResults.Items.Add("Sky " + ws.sky); lbResults.Items.Add("Temperature " + ws.temp); lbResults.Items.Add("Humidity " + ws.humidity); lbResults.Items.Add("Barometer " + ws.pressure); lbResults.Items.Add("Visibility " + ws.visibility); } If you compare the previous code with the synchronous calling code, you should find some significant changes. When you add a Web reference, the proxy class includes Begin and End methods for each Web method. In this case, those are the BegingetSummary() and EndgetSummary() methods. The Begin method takes all the same parameters as the underlying Web method, plus two others. The first is the address of a callback method, and the second is an object whose properties should be available in the callback method. When you call the Begin method, the .NET Framework launches the call to the Web service in the background and returns an IAsyncResult object that represents the status of the asynchronous operation. After the call to the Begin method, you can add any code you want to run parallel to the asynchronous operation. To wait for the asynchronous operation to complete before the Web response is completed, you can call the WaitHandle.WaitOne() method of the IAsyncResult object. When the Web method call completes, the Callback() method is invoked. You then retrieve the original object and use its End method to finish the work of using the Web service.
|