Exposing your data using .NET WCF Data Services: Part 1

Last year I wrote the post Data as a Service: The next big thing? where I mentioned that in a *Devices + Services era, *one issue we face is that in many cases it is not possible to access useful data to build consumer applications over. I summed up that post by saying the following:

As a result I really think that for the Devices + Services to succeed we need more data to consume… if you have data that can be shared to make other services possible, simpler or better then share it! It’s time to make DaaS the next big thing along side Devices + Services.

With that in mind, the question is: Is there a feasible way for companies/organisations with data available to share to expose their data? I’ve been wondering about that myself recently. I had heard of OData but never really looked much into it and although I’m still a newbie to OData it turns out that OData provides a simple, uniform way of publishing data. In .NET this can be done using WCF Data Services and below I provide a walkthrough of publishing a small database using WCF Data Services and consuming that in an application.

Creating a Simple Database

For this post I decided to build a simple quotes database for the walkthrough. I’ve built a small database of quotes by putting together quotes I’ve found on the internet from people like Bill Gates, Steve Jobs and others.

Quotes DB

As can be seen above, the table contains three basic columns to hold the quote ID, Author and the Quote text. The next thing that needs to be done is creating a way to use that data which we can do using WCF Data Services.

Creating a WCF Data Service

You can host a WCF Data service in a web application so given that for this post all we need is the service I have just added it to a blank ASP.NET Web app by using the following steps

1) Create an Empty ASP.NET Web Application in Visual Studio

Launch Visual Studio and add a new ASP.NET Empty Web application.

Create an empty Web application

Web Application Files

2) Add your Data Model to the project created in the previous step

The next thing that needs to be done is adding our Data Model so that we can use that in our service. One way to do this is by adding an ADO.NET Entity Data Model as shown in the screenshots below.

Add Entity Data Model

For this post we’ll use the Quotes database that was created earlier by choosing theGenerate from database option.

Generate from Database

Set connection string

Once we have established a connection to our database we can then choose the desired tables and when completed our model will be added to the solution as seen below.

Select Entities

Entity Files

3) Assigning our Data Model to a Service

With our Data Model ready we can now create the service to consume this model with. This can be done by adding a WCF Data Service file to the project. In the screenshots below I create a* QuotesDataService.svc *file.

Add a service file

Once this file is created the code below will be presented.

Data Service default code

Now what we need to do is assign our Data Model entities to the DataService and also set up the access rules to the service. Below is what I’ve done for the Quotes service.

Data service code with entity assigned

As you can see above, I’ve passed the QuotesDBEntities model – the data source class name – to the DataService and set read access to the operations and data.

Accessing and Querying the Data Service

The previous steps complete everything required for us to be able to access the data and now we are ready to start querying the service.

1) Browse to the service URL

Given we are currently still running locally all we need to do is just run the Visual Studio Project with the QuotesDataService.svc page set as the start page. When this is done we are presented with the following page showing us the available entities in this service that we can query.

Published service

2) Perform some queries on the Data

With our service published we can now start querying and below are some example queries I have done.

Show all available quotes

Published service queries

Show a quote with a specific ID

Published service queries

Show quotes by a specific Author

Published service queries

The above queries demonstrate a few ways OData can be used to query the data published in our service. You may have observed that in this instance the data is displaying in an ATOM feed format. WCF Data Service also enables you to publish that data using JSON. One way to do this is by changing the response headers to accept the JSON format which you can do through your code/fiddler. Another way is to use the $formatattribute. Unfortunately this is not supported in WCF Data Services out of the box. The good news is, there’s a way to add that support!

Adding JSON format support to your WCF Data Service

In addition to publishing the service in ATOM format, here is one way we can add JSON support to the WCF Data service.

From the link above I went with the option of using JSONP and URL-controlled format support for ADO.NET Data Services which required me to do the following:

1) Download the code

Download the extension code from here

2) Add the JSONPSupportBehavior.cs class to your service project

From the files extracted in the previous step, the next action required is to add this file to your project so that the extension can be used. I’ve done this by just copying and including the file in the solution as shown below.

JSON Support extension file

3) Assign the [JSONPSupportBehavior] attribute

By adding the class in the previous step we are now able to annotate our service class with this property so that we can publish our data in JSON format. To do so all that needs to be done is to bring in the required namespace and assign the attribute to the class as shown below.

JSON Support code

This completes the required steps and we can now access our service data using JSON format.

4) Viewing the service data using JSON

With the addition of the code mentioned in the previous step completed, we can now use the $format option to view the data using JSON as shown below. Almost!

JSON Support error

As you can see, when I try this and pass the $format=json in my URL I get the response above instead of the actual JSON. The reason for this is that I’m using the latest version of WCF Data Services. The changes we made earlier to support the format query will only work directly if you’re using WCF Data Services older than version 5. However, we can work around this issue by either adding the odata=verbose option or setting the MaxDataServiceVersion header to 2. To fix this issue in my case, I went with the former option by updating the JSONSupportBehaviour extension code to add the verbose option to the AfterReceiveRequestmethod as shown below.

JSON Support error fix

With the above steps completed we can finally request our service data in JSON format as shown below.

JSON Data display

Summary

The steps above in this post demonstrated how we can use WCF Data Services to easily publish a data model so that its data can be consumed by other applications through OData. This enabled us to expose the data using both the ATOM and JSON formats. In the next post I will continue on from this post by publishing this data into the cloud using Windows Azure so that it can then be consumed by client applications such a Windows 8 or Windows Phone 8 app.