ramlicious Blogs by Tina & Prabhu

September 14, 2011

OData and JSON

Filed under: JSON,OData,Visual Studio,WCF,XML — Prabhuram @ 1:18 pm

In my last two article which talks about developing WCF data services, you would have noted that when you browsed the .svc page (http://localhost/ODataSampler.Web/MusicAlbum.svc/) from the browser the XML appears something like this:

<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<service xml:base="http://localhost/ODataSampler.Web/MusicAlbum.svc/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2007/app">
    <collection href="Albums">

This is the AtomPub format (yes the same format weblogs are distributed). OData supports AtomPub and JSON. In this article we will quickly see how JSON format. In this case, I will use Netflix source. I’ve created a very simple HTML page to demostrate the power of OData.

HTMLPage.htm (976.00 bytes)

Simply click open the link and open the file in a browser, and check out what happens. All that the file contains is a simple call to pull the JSON data from Netflix. And you can also see that HTML only has very simple JQuery calls to pull the data. And when you view the source, it doesn’t have any movie data on the client side, and so everything happens on the fly.

Here is the URI used in the HTML file


pulls the movie titles released in 2011, and the requested data format is json. So if you just click open the link you can see the actual json data.

Using OData Services

Filed under: C#,OData,Visual Studio,WCF — Prabhuram @ 7:26 am

After writing WCF Data Services with an example (OData Sample), I realized that I could have showed the service that we just created with an example by calling the Data Service from a console application, so I created a console application and then added a service reference to the .svc source and then simply able to call the data service.

    class Program
        static void Main(string[] args)
            foreach (var p in new MusicAlbumService(new Uri("http://localhost/ODataSampler.Web/MusicAlbum.svc/")).Albums)

How easy is that!!

WCF Data Services with an example (OData Sample)

Filed under: C#,OData,Visual Studio,WCF — Prabhuram @ 3:42 am

I did this example with VS2008. Have this pre-requisites in place for working with OData services

  • Visual Studio 2008 with SP1 (required for ADO.NET Entity Model)
  • .NET 3.5 with SP1
  • IIS 7.0 (I was using a Win7 station)
  • Run this command from VS command prompt if you don’t have a HTTP handler for .SVC in IIS.
    %WinDir%\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\servicemodelreg –i
  • Always have this super tool LINQPad.exe. You guys will need this all the time.

This article is for the novice users to jumpstart, experiment and learn about the ADO.NET Data Services (formerly code named Astoria and called WCF Data Services in .NET 4.0). First create a Web application and add two simple classes.

namespace MyServices
    [DataServiceKey("Name")] //To avoid Request Error, introduce a Primary Key
    public class MusicAlbum
        public string Name { get; set; }
        public string Artist { get; set; }
        public int Year { get; set; }
        public MusicAlbum(string Name, string Artist, int Year)
            this.Name = Name;
            this.Artist = Artist;
            this.Year = Year;
    public class MusicAlbumService
        List list = new List(){
            new MusicAlbum("Eagles", "Eagles", 1972),
            new MusicAlbum("Queen", "Queen", 1973)};
        public IQueryable Albums
                return list.AsQueryable();

And introduce your Data Service Page by adding ADO.NET Data Service from Add New Item. Make the following changes by introducing the MusicAlbumService as the Data Service.

public class WebDataService : DataService< MusicAlbumService >
    public static void InitializeService(IDataServiceConfiguration config)
         config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);

Compile this project and host MusicAlbum.svc. Now we will test this data source in LINQPad, and so we will add a connection to the .svc file.

And note that you can query the database like:

from p in Albums select p

To give a better understanding WCF Data Service is basically an implementation of OData (Open Data Protocol). Also you can make changes to the query like this and execute:

from p in Albums where p.Name == "Eagles" select p

and the reason why I used LINQPad is to show the SQL tab that shows you the SQL equivalent


As you can see, you can copy the URI and paste it in a browser to see the results over the web.

So you learnt:

  • With in short time you are able to expose a datasource (without a database) to outside world
  • And you are able to achieve this over the web on HTTP as a URI
  • The metadata of the data service is available to the client (that way LINQPad was able to understand about MusicAlbum)
  • Ofcourse it is interoperable
  • A relational data model approach
  • Moreover it allows to apply business logic from the client by including custom filter criterias
  • There are multiple implementation of OData across multiple platforms and for the .NET developers it is part of .NET3.5+

Download the complete VS solution from here: ODataSampler.Web.zip (3.51 mb)

Query against those OData sources that are availableover the internet like eBay, and Netflix.

Powered by WordPress