Building LOB Applications : Accessing RESTful Data using OData

2/5/2011 5:26:37 PM

1. Silverlight LOB Enhancements

Silverlight 3 introduced additional controls for line-of-business (LOB) application development. In addition, Silverlight 3 introduced the new Silverlight Navigation Application project template, which provides a great starter application for a LOB application.

Silverlight 4 builds on these capabilities with improved data-oriented controls as well as new controls such as the RichTextBox control to display, enter, and edit rich text and the WebBrowser control for hosting HTML within an out-of-browser (OOB) application. Speaking of which, Silverlight 4 greatly improves OOB functionality to enhance LOB development.

  • HTML hosting in the WebBrowser control

  • Pop-up alerts with the NotificationWindow class

  • Support for elevated trust to the underlying platform

Other LOB-related enhancements in Silverlight 4 are drag-and-drop, clipboard access, right-click mouse events, and printing. A major step forward is much better support for Commanding, which allows developers to more easily implement Model-View-ViewModel applications when building data-driven applications.

2. Data Access Enhancements

Silverlight 4 includes strong network service capabilities . In this article, we highlight additional networking support for accessing data in a structured way via both WCF Data Services and WCF RIA Services. The next two sections provide a high-level overview of these two technologies. For a deeper discussion on these technologies and how they relate to other SOAP and REST-based development models, please go to:

2.1. WCF Data Services

Silverlight 4 includes rich support for accessing web data either on the Internet or intranet via WCF Data Services. Formerly known as ADO.NET Data Services in Silverlight 3, WCF Data Services provides support for REST-based data access as well as support for the new Open Data Protocol (OData) format. You can learn more about OData at

Silverlight 4 introduces several improvements for WCF Data Services including a new DataServiceCollection class that provides better data binding support with automatic updates to bound controls and greatly simplifying REST development in Silverlight. WCF Data Services also supports both out-of-browser and cross-domain execution in Silverlight 4.

2.2. WCF RIA Services

When you think LOB applications, you automatically think n-tier development. Introduced at Mix 09 in beta as Microsoft .NET Rich Internet Applications (RIA) Services, the name has changed to WCF RIA Services but the goal is the same, which is to simplify the development of n-tier solutions for Silverlight RIA applications. WCF RIA Services is automatically installed when you download and install the Silverlight 4 Tools for Visual Studio 2010.

The challenge with n-tier applications is flowing data across application tiers with support for edits, validation, and so on.

WCF RIA Services provides a bridge between Silverlight 4's rich presentation-tier capabilities and ASP.NET's powerful middle-tier capabilities on the server, including support for ASP.NET authentication and user settings management.

To get started with WCF RIA Services, you create classes in the middle-tier web application that inherit from the System.Web.DomainServices.DomainService base class to define a set of operations on resources such as an ADO.NET Entity Model or other resources such as a user registration service. You can have classes on the middle tier be available on the client-side in Silverlight by adding the EnableClientAccess attribute to the middle-tier classes. WCF RIA Services will automatically generate the corresponding client-side code into the presentation tier (the Silverlight application). At this point, you can code against the generated client-side code without worrying about tracking and packaging up changes and so forth to pass back to the middle tier. WCF RIA Services manages this for you.

3. Accessing RESTful Data using OData

3.1. Problem

You need to access web data in your Silverlight application.

3.2. Solution

Take advantage of existing REST and OData end-points either on the intranet or Internet.

3.3. How It Works

Silverlight 4 includes a client development model that enables accessing REST and OData end-points by simply using the Add Service Reference feature in Visual Studio 2010 to create the necessary client-side representations of the server-side data.

To find your Web Data, navigate over to the site and click Producers to see what sample data is available. You will connect to the OData Test read-only dataset available at

3.4. The Code

To get started, right-click on Recipe 1 in Visual Studio and select Add Service Reference and enter the URL for the sample OData service as shown in Figure 1.

Figure 1. Recipe 1 Adding an OData service reference

Clicking OK generates the necessary client-side code files to enable access to the service. Next, add a using reference to bring in the client-side code:

using Ch09_LOBApplications.Recipe9_1.OdataDemoReadOnlyServiceReference
using System.Data.Services.Client

Then, add a MainPage_Loaded event handler on the main page where you do three things:

  • Initialize the data service context to connect to the service

  • Create a collection to store the data

  • Define a handler for the LoadCompleted event for the collection when loaded

Here is the MainPage_Loaded event handler containing these steps:

private void MainPage_Loaded(object sender, RoutedEventArgs e)
context = new DemoService(
new Uri("",
Products = new DataServiceCollection<Product>(context);
Products.LoadCompleted +=
new EventHandler<LoadCompletedEventArgs>(Products_LoadCompleted);

As you can see, you configure the context variable to point to the OData test service. Next, you instantiate a new DataServiceCollection with the Product type and assign a LoadCompleted event handler. Then you add a Button to kick off loading the data and a DataGrid to display the data in the UI. The LoadCompleted event fires when the GetProductsBtn is clicked.

In the GetProductsBtn Button event, you create a LINQ query to retrieve all of the products and then pass the query in to the LoadAsync method on the Products variable of type DataServiceCollection<Product>. The LoadAsync method uses the context variable to know where to retrieve the data from. Here is the GetProductsBtn_Click event handler:

private void GetProductsBtn_Click(object sender, RoutedEventArgs e)
ProductsGrid.DataContext = null;

var ProductsQuery = from products in context.Products
select products;


When LoadAsync completes, the DataServiceCollection.LoadCompleted event is fired on the Products variable, which checks for an error; if no errors are present, it assigns the data to the ProductsGrid.ItemSource property as shown in Listing 9-1.

Listing 1. The Recipe 1 Products_LoadCompleted Method
void Products_LoadCompleted(object sender, LoadCompletedEventArgs e)
if (e.Error == null)
// Load all pages of Orders before binding.
if (Products.Continuation != null)

// Bind the root StackPanel element to the collection;
// related object binding paths are defined in the XAML.
ProductsGrid.ItemsSource = Products;

// Re-enable the button since the loading is complete.
GetProductsBtn.IsEnabled = true;

// Set the focus to the first order, which loads the related items.
ProductsGrid.SelectedIndex = 0;
MessageBox.Show(string.Format("An error has occured: {0}", e.Error.Message));
GetProductsBtn.IsEnabled = true;

The result of this code is shown in Figure 2.

Figure 2. Recipe 1 UI after loading data

  •  Programming with DirectX : Additional Texture Mapping - Image Filters
  •  Microsoft XNA Game Studio 3.0 : Making a Game Program
  •  iPhone 3D Programming : Textures and Image Capture - Texture Compression with PVRTC
  •  iPhone 3D Programming : Textures and Image Capture - Texture Formats and Types
  •  iPhone 3D Programming : Textures and Image Capture - Fight Aliasing with Filtering
  •  iPhone 3D Programming : Textures and Image Capture - Texture Coordinates Revisited
  •  Programming with DirectX : Additional Texture Mapping - Sprites
  •  Programming with DirectX : Additional Texture Mapping - Alpha Mapping
  •  Microsoft XNA Game Studio 3.0 : Writing Your First Program (part 2) - Running the Same XNA Game on Different Devices
  •  Microsoft XNA Game Studio 3.0 : Writing Your First Program (part 1)
  •  Programming with DirectX : Shading and Surfaces - Additional Texturing Topics
  •  iPhone 3D Programming : Adding Textures to ModelViewer (part 4) - Enabling Textures with ES2::RenderingEngine
  •  iPhone 3D Programming : Adding Textures to ModelViewer (part 3) - Enabling Textures with ES1::RenderingEngine
  •  iPhone 3D Programming : Adding Textures to ModelViewer (part 2) - Generating Texture Coordinates
  •  iPhone 3D Programming : Adding Textures to ModelViewer (part 1) - Enhancing IResourceManager
  •  Programming with DirectX : Shading and Surfaces - Implementing Texture Mapping (part 2) - Multi Texture Demo
  •  Programming with DirectX : Shading and Surfaces - Implementing Texture Mapping (part 1) - 2D Texture Mapping Demo
  •  Building Out Of Browser Silverlight Applications - Using COM Interoperability and File System Access
  •  Building Out Of Browser Silverlight Applications - Controlling the Application Window
  •  iPhone 3D Programming : Adding Depth and Realism - Loading Geometry from OBJ Files
    Top 10
    Nikon 1 J2 With Stylish Design And Dependable Image And Video Quality
    Canon Powershot D20 - Super-Durable Waterproof Camera
    Fujifilm Finepix F800EXR – Another Excellent EXR
    Sony NEX-6 – The Best Compact Camera
    Teufel Cubycon 2 – An Excellent All-In-One For Films
    Dell S2740L - A Beautifully Crafted 27-inch IPS Monitor
    Philips 55PFL6007T With Fantastic Picture Quality
    Philips Gioco 278G4 – An Excellent 27-inch Screen
    Sony VPL-HW50ES – Sony’s Best Home Cinema Projector
    Windows Vista : Installing and Running Applications - Launching Applications
    Most View
    Bamboo Splash - Powerful Specs And Friendly Interface
    Powered By Windows (Part 2) - Toshiba Satellite U840 Series, Philips E248C3 MODA Lightframe Monitor & HP Envy Spectre 14
    MSI X79A-GD65 8D - Power without the Cost
    Canon EOS M With Wonderful Touchscreen Interface (Part 1)
    Windows Server 2003 : Building an Active Directory Structure (part 1) - The First Domain
    Personalize Your iPhone Case
    Speed ​​up browsing with a faster DNS
    Using and Configuring Public Folder Sharing
    Extending the Real-Time Communications Functionality of Exchange Server 2007 : Installing OCS 2007 (part 1)
    Google, privacy & you (Part 1)
    iPhone Application Development : Making Multivalue Choices with Pickers - Understanding Pickers
    Microsoft Surface With Windows RT - Truly A Unique Tablet
    Network Configuration & Troubleshooting (Part 1)
    Panasonic Lumix GH3 – The Fastest Touchscreen-Camera (Part 2)
    Programming Microsoft SQL Server 2005 : FOR XML Commands (part 3) - OPENXML Enhancements in SQL Server 2005
    Exchange Server 2010 : Track Exchange Performance (part 2) - Test the Performance Limitations in a Lab
    Extra Network Hardware Round-Up (Part 2) - NAS Drives, Media Center Extenders & Games Consoles
    Windows Server 2003 : Planning a Host Name Resolution Strategy - Understanding Name Resolution Requirements
    Google’s Data Liberation Front (Part 2)
    Datacolor SpyderLensCal (Part 1)