MULTIMEDIA

Building LOB Applications : Using Visual Studio 2010 WCF Data Services Tooling

2/5/2011 5:30:28 PM

1. Problem

You have a database that you want to make available via REST and OData programming models.

2. Solution

Take advantage of Visual Studio 2010 tooling to publish a database via WCF Data Services and the OData protocol and then access the database from Silverlight 4.

3. How It Works

You already saw the tooling on how to add a Service Reference to an OData service using Visual Studio 2010 in Recipe 1. In this recipe, you learn how to make a database available as a service using the tooling in Visual Studio 2010 and then access that service from Silverlight.

4. The Code

Start by creating an ADO.NET Entity Framework Data Model named Northwind.edmx that contains all of the tables in Northwind in a folder named DataModel in the ASP.NET TestWeb project. Next, create a folder named DataService in the ASP.NET TestWeb project and then add a WCF Data Service item named NorthwindDataService.svc to the DataService folder. Here is the generated data service code:

public class NorthwindDataService :
DataService</* TODO: put your data source class name here */ >
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
// TODO: set rules to indicate which entity sets and
// service operations are visible, updatable, etc.
// Examples:
// config.SetEntitySetAccessRule("MyEntityset",EntitySetRights.AllRead);
// config.SetServiceOperationAccessRule(
// "MyServiceOperation", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
}


You perform the first TODO mentioned in the code by replacing the comment in the constructor with the type TestWeb.DataModel.NorthwindEntities to make the entities available via the Data Service. Next, you edit the InitializeService method to configure access rules for your entities by granting all rights to all entities in this line of code:

config.SetEntitySetAccessRule("*", EntitySetRights.All);

When you run the service to display it in code, it looks like Figure 1.

Figure 1. Recipe 2 Northwind Data Service

To view just the Orders data, you can alter the URL by appending Orders to the end like this:

http://localhost:64524/DataService/NorthwindDataService.svc/Orders

Figure 2 shows the results as a feed.

Figure 2. Recipe 2 Northwind Data Service Orders feed

To view the feed as XML, go to Internet Explorer Tools | Internet Options | Content | Feed and Web Slice Settings | and uncheck Turn on feed reading view. Figure 3 shows the feed as XML.

Figure 3. Recipe 2 Northwind Data Service Orders feed as XML

Next, select Recipe 2 and right-click to bring up the Add Service Reference menu and click Discover to automatically find the NorthwindDataService in the TestWeb project. Give the service a namespace of NorthwindDataServiceReference and click OK to add the client side code to access the data service.

Add a using clause to MainPage.xaml.cs to bring in the service namespace. The code is similar to that in Recipe 1 and is shown in Listing 2.

Listing 2. The Recipe 9-2 MainPage.Xaml.cs Codebehind File
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Ch09_LOBApplications.Recipe9_2.NorthwindDataServiceReference;
using System.Data.Services.Client;

namespace Ch09_LOBApplications.Recipe9_2
{
public partial class MainPage : UserControl
{
NorthwindEntities NorthwindContext;
DataServiceCollection<Customer> Customers;

public MainPage()
{
InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
NorthwindContext = new NorthwindEntities(
new Uri("DataService/NorthwindDataService.svc", UriKind.Relative));
Customers = new DataServiceCollection<Customer>(NorthwindContext);
Customers.LoadCompleted +=
new EventHandler<LoadCompletedEventArgs>(Customers_LoadCompleted);
}

void Customers_LoadCompleted(object sender, LoadCompletedEventArgs e)
{
if (e.Error == null)
{
// Load all pages of Orders before binding.
if (Customers.Continuation != null)
{
Customers.LoadNextPartialSetAsync();
}
else
{
// Bind the root StackPanel element to the collection;
// related object binding paths are defined in the XAML.
NorthwindDataGrid.ItemsSource = Customers;
NorthwindDataGrid.UpdateLayout();

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

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

private void GetCustomersBtn_Click(object sender, RoutedEventArgs e)
{
NorthwindDataGrid.DataContext = null;

var CustomersQuery = from customers in NorthwindContext.Customers
select customers;

Customers.LoadAsync(CustomersQuery);
}
}
}


Figure 4 shows the data displayed in the final user interface.

Figure 4. Final UI for Recipe 2

Other  
  •  Building LOB Applications : Accessing RESTful Data using OData
  •  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
  •  
    Video
    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)