Invoking a RESTful Service with WebClient

The following example will demonstrate the steps required to invoke a RESTful web service using WebClient. You can find the complete example code in the book’s code folder for this chapter. The sample displays a Silverlight 2 client application’s DiggStoryView.xaml control, shown in Figure 7-5, which allows the user to retrieve the most recent stories from the RESTful Digg.com web service.

The DiggStoryView.xaml control

Figure 7-5. The DiggStoryView.xaml control

The WebClient class invokes asynchronous operations that fire off events when they have completed their respective tasks. Some of the asynchronous operations also fire events that contain information regarding the progress of the asynchronous operations. WebClient contains the asynchronous operations shown in Table 7-1.

Only one request can be made to an asynchronous operation at a time using WebClient. If a second asynchronous operation is invoked, an exception will be thrown. When event handlers are added to the WebClient class’s events, the code in the handler executes in the UI thread, not in the background thread. This is significant because by operating on the UI thread, any code executing in the event handler can get values, set values, and interact with controls in the Silverlight client application’s UI without any special coding. The WebClient API has a very simple interface that makes it easy ...

Get Data-Driven Services with Silverlight 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.