OData and WCF Data Services 

Tags:

This week we started investigating OData and WCF data services as a way to easily expose and query a remote database.

OData, the Open Data Protocol is a protocol for querying and updating data over using Http and AtomPub

Visual studio 2010 and .NET 4.0 come with rich support to both produce and consume data using the Open Data Protocol.

This post is a tutorial on how to set up a web service that exposes OData, and how to query it from a client application.

Prerequisites

  • Visual Studio 2010
  • .NET 4.0  Framework or the Data Services update for Microsoft .NET Framework 3.5 Service Pack 1

Exposing data as Odata

 

Create a new ASP.NET Web application

image

 

Create the Entity Data Model for the Northwind database

The Entity Data Model will be the base for the WCF Data Service that will expose the Northwind data in the OData format

image

image

image

Here we can choose which Tables, Views and Stored Procedures will be exposed

Finish creates the Entity Data Model for the Northwind database

image

 

Create a new WCF Data Service

image

 

image

 

The NorthwindService inherits from the DataService with as generic parameter the EntityDataModel we want to expose, in this case the NorthwindEntities we just created.

By default noone can do anything, we first need to set access rules for our entities.

The easyest way to do this is to expose all entities with read-all rights:

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

If the Entity Data Model is in a different assembly you’ll need to add a reference, and make sure the connectionstring is in the web.config of the referencing project

 

Visiting the service now shows the exposed data:

image

 

 

Consuming the data

To use the data exposed by the web service we create a client application.

Add a Service Reference to the web service.

For testing, there is always a public Northwind web service available on http://services.odata.org/Northwind/Northwind.svc/

image

This generates a web service proxy.

New is that this also generates a DataServiceContext.

Using the DataServiceContext we can query the data service through a linq provider.

The following extremely simple example print a list with the Name and Unit Price of all available products.

 

class Program
{
   static void Main(string[] args)
   {
       string url = "http://localhost:40171/NorthwindService.svc/";
       var context = new NorthwindEntities(new Uri(url));

       var someProducts = from p in context.Products
                          where !p.Discontinued
                          select p;

       foreach (var product in someProducts)
       {
           Console.Out.WriteLine("{0} - {1}",product.ProductName,product.UnitPrice);
       }
       Console.Read();
   }
}

image

Download the sample code:

http://www.orbitone.com/SiteCollectionDocuments/Northwind%20OData%20Service%20Sample.zip

 

More information, samples, articles and tutorials can be found on http://www.odata.org/

Some more links to get you started:

 
Posted by Mel Gerats on 9-Jun-10
2 Comments  |  Trackback Url | Bookmark this post with:        
 

Comments


Tom Pester commented on Wednesday, 9-Jun-2010
Some more links to get you started : Open Data Protocol Visualizer http://visualstudiogallery.msdn.microsoft.com/en-us/f4ac856a-796e-4d78-9a3d-0120d8137722 <Open Data Protocol (OData) with Pablo Castro/> http://www.hanselminutes.com/default.aspx?ShowID=223 Episode 43: Talking OData and SQL Modeling with Douglas Purdy http://deepfriedbytes.com/podcast/episode-43-talking-odata-and-sql-modeling-services-with-douglas-purdy/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+deepfriedbytes+%28Deep+Fried+Bytes%29&utm_content=Google+Reader Carl and Richard get the word on oData from Brad Abrams, Bob Dimpsey and Lance Olson. http://www.dotnetrocks.com/default.aspx?ShowNum=519 Using Data Services over SharePoint 2010 – Part 1 – Getting Started http://blogs.msdn.com/b/astoriateam/archive/2009/10/21/using-data-services-over-sharepoint-2010-part-1-getting-started.aspx The Open Data Protocol (OData) http://www.stephenforte.net/PermaLink,guid,28df55a8-8811-4c4b-b319-75c5c58d1444.aspx


Tom Pester commented on Wednesday, 9-Jun-2010
first paragraph: test from IE8 first paragraph: test from IE8first paragraph: test from IE8 first paragraph: test from IE8 second paragraph : test from IE8 second paragraph : test from IE8 second paragraph : test from IE8 second paragraph : test from IE8

Your name  *
Email  *
Your URL 
Comment  *

Please enter the text from the image


Orbit One on LinkedIn
Contact us - Raas Van Gaverestraat 83, 9000 Gent, Belgium - Tel. +32 (9) 330.15.00 - Privacy Statement - Sitemap - Sign In Developed with Microsoft Office SharePoint Server 2007