Understanding Web Services

The key to understanding Web services is knowing something about the protocols that make them possible, which are as follows :

  • Simple Object Access Protocol (SOAP)

  • Disco and Universal Description, Discovery, and Integration (UDDI)

  • Web Services Description Language (WSDL)

SOAP

For 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 UDDI

Before 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.

WSDL

Another 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:

  • The data types it can process

  • The methods it exposes

  • The URLs through which those methods can be accessed

graphics/alert_icon.gif

Although the WSDL files enable interaction with Web services without any prior knowledge, these files are not required for a Web service to function. You can make a Web service available on the Internet without any WSDL files. In that case, only clients who already know the expected message formats and the location of the Web service are capable of using it.


Invoking a Web Service

Now 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:

  1. Create a blank solution using Visual Studio .NET. Name the solution 315C09 and specify its location as C:\Inetpub\ wwwroot \ExamCram . (You can modify the location to match your configuration.)

  2. Add a new Visual C# ASP.NET Web application project to the solution. Specify http://localhost/ExamCram/315C09/Example9_1 as the location of the project.

  3. Right-click the References folder in Solution Explorer and select Add Web Reference. This opens the Add Web Reference dialog box. Type http://live.capescience.com/wsdl/AirportWeather.wsdl in the Address bar of the Add Web Reference dialog box and press Enter. This connects to the Airport Weather Web service and downloads the information shown in Figure 9.1.

    Figure 9.1. The Add Web Reference dialog box enables you to connect to a Web service over the Internet.

    graphics/09fig01.jpg

  4. Click the Add Reference button to add a reference to the Airport Weather Service in your project.

  5. Open the Web form and place a Label control, TextBox control ( txtCode ), Button control ( btnGetWeather ), and Listbox control ( lbResults ) on the form. Change the Text property of the Label control to Airport Code: .

  6. Double-click the Button control and enter the following code to invoke the Web service when the user clicks the Get Weather button:

     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 service to get a summary object     com.capescience.live.WeatherSummary ws =         aw.getSummary(txtCode.Text);    // 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); } 
  7. Select Debug, Start to execute the Web form and enter a four-digit ICAO airport code (such as KDTW for Detroit). Then click the Get Weather button. After a brief pause while the Web service is invoked, you'll see the current weather at that airport in the list box, as shown in Figure 9.2.

    Figure 9.2. You can invoke a remote Web service and access the objects returned by it.

    graphics/09fig02.jpg

graphics/note_icon.gif

You can find a list of four-letter ICAO airport codes to use with this Web service at www.ar- group .com/icaoiata.htm. Codes for airports in the United States all start with K; codes for Canadian airports all start with C.


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.

graphics/alert_icon.gif

When a client program calls a Web service asynchronously, the execution control returns to the client program without waiting for the complete execution of the Web service. This approach of invoking Web services helps make the user interface responsive .


graphics/alert_icon.gif

If you have multiple outstanding asynchronous Web method calls, you can wait for them all to come back by using the static WaitHandle.WaitAll() method. Or you can wait for any first one to return by using the static WaitHandle.WaitAny() method.




MCAD Developing and Implementing Web Applications with Visual C#. NET and Visual Studio. NET (Exam [... ]am 2)
MCAD Developing and Implementing Web Applications with Visual C#. NET and Visual Studio. NET (Exam [... ]am 2)
ISBN: 789729016
EAN: N/A
Year: 2005
Pages: 191

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net