Practical ASP.NET

Creating a WCF Data Service

If you want to integrate server-side Web service processing with AJAX objects in your browser, you should be considering WCF Data Services -- especially if you're working with the Entity Framework. Here's a quick introduction to the technology.

In my last column (AJAX Data Strategies in .NET 4), I outlined my current project: Using the tools in the latest version of the AJAX library -- primarily the dataContext and dataView -- to create an application like the one discussed in an earlier jQuery-themed article (Integrating jQuery, Web Services, AJAX and ASP.NET) with full CRUD support, displaying both single and multiple records.

As I discussed in my last column, I'll be using Entity Framework to handle my database updates on the server. To further reduce the server-side code, I'm going to have my WCF Service just return and accept EF Entities. However, as I also discussed, you can't return an EF Entity from a plain old WCF service -- you have to use WCF Data Services if you want to send EF Entities to the client.

In this column, I'm going to set up the WCF Data Service that I'll need for the project. While I'm using Visual Studio 2010 and .NET 4, WCF Data Services was included in Service Pack 1 for Visual Studio 2008 (though it was called ADO.NET Data Services). If, however, you're not interested in this technology and would prefer to use an ASMX or WCF service returning Data Transfer Objects, feel free to tune out and come back for my next column.

Adding a WCF Data Service to Your Site
To create a WCF Data Service in Visual Studio 2010, just select the WCF Data Service template from the Add New Item dialog. You'll get a new svc file added to your project with an associated code file containing a class that inherits from the DataService class.

Your first step is to tie your class to your data source through the Inherits statement. In my case, I'm using an Entity Framework model called northwindEntities as my data source, so I alter the Inherits statement to look like this:

Public Class NorthwindDataServices
    Inherits DataService(Of northwindEntities)

You're not obliged to use EF -- you can also bind a WCF service to your own custom classes.

That's pretty much all the code you have to write to retrieve and update data. The service will now return objects from your data source based on the URL you use to access it -- you just embed Entity names in the URL.

However, by default no access is allowed to your DataService. Your second step, then, is to enable access. You enable access by using methods on the DataServiceConfiguration object passed to your DataService's IntializeService method. For this project, I'll just enable everything for everyone:

Public Shared Sub InitializeService(
       ByVal config As DataServiceConfiguration)
  config.SetEntitySetAccessRule("*", EntitySetRights.All)
End Sub

Asking for Data
You can test your service by right-mouse clicking on your svc file and selecting "View in Browser." By default, Internet Explorer will recognize all of the XML coming from your service as a type of RSS feed and display it poorly (mostly, not at all).

To see the results of your service in Internet Explorer, you'll need to go into Tools | Internet Options | Content and, in the Feeds section, click on the Settings button. In the resulting Feeds Settings dialog uncheck "Turn on feed reading view." Now you can ask for a particular Entity and see something interesting. This example, for instance, displays all the Customers for my EF model, which is built on the Northwind database:

http://MyServer/MySite/NorthwindDataServices.svc/Customers

This will display just the data for customer ALFKI:

http://MyServer/MySite/NorthwindDataServices.svc/Customers('ALFKI')

To do more, you can place special keywords in the URL's querystring. The $filter keyword allows you to specify criteria for returning data, the $orderby lets you sort the results, and so on.

You can extend your DataServices in a variety of ways. To support debugging you should add this override to catch exceptions:

Protected Overrides Sub HandleException(ByVal args As HandleExceptionArgs)
Dim Message As String = "Error: " & args.Exception.Message
  If args.Exception.InnerException IsNot Nothing Then
    Message &= ", " & args.Exception.InnerException.Message
  End If
  args.Exception = New DataServiceException(400, "", Message, 
                     "en-US", args.Exception)
End Sub

There's much more, of course, but this is all I need to start building my AJAX-enabled application. In my next column, I'll start looking at the code in my ASP.NET page.

About the Author

Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.

comments powered by Disqus

Featured

Subscribe on YouTube