ramlicious Blogs by Tina & Prabhu

September 14, 2011

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
        {
            get
            {
                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

http://localhost/ODataSampler.Web/MusicAlbum.svc/Albums('Eagles')

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.

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress