When I first saw the WCF OData Service template in Visual Studio 2010, really I thought, what is the need for OData Services. Because we already have Web Services, WCF Services and REST Services.
But then I was able to understand why when I went through the OData concept.
In this article, I would like to explain the concept of OData. Before starting coding, first I will look into the basics of OData.
What is OData?
OData is the Open Data Protocol. It is an open web protocol started by Microsoft to expose data using existing web technologies. This is based on Representational State Transfer (REST) full architecture. HTTP, AtomPub (similar to RSS, but specifically introduced special XML format for OData), JSON are all supported.
Here, the URL is the primary operator on the data query.
WCF OData Service
In Visual Studio 2010, there is a template to create a WCF OData service. So, we can easily create an OData Service using Visual Studio 2010.
Example of OData Service has been used
- Last (2010) football world cup (S.A), the scoring website was done using an OData Service.
Why should we use an OData Service?
- OData is based on the REST architecture, so we can retrieve data based on an URL.
- It also supports HTTP, Atom Pub as well as JSON format.
- It has support for any type of data source. Even you can use a custom class as a data source.
- No need to create a proxy service object. So, it is lightweight to use.
- You can create your own custom methods and expose it.
- Since it is lightweight, the interaction between server and client is fast. Thus, performance is good.
- It offers full CRUD support by using the different HTTP methods:
- GET: Gets one or many entries.
- POST: Create a new entry.
- PUT: Update an existing entry.
- DELETE: Remove an entry.
- A WCF Data service can expose an entity model via an URI.
- A WCF Data service could be consumed by any type of client like Windows, SilverLight, Web, AJAX, and console.
Limitations
- Since it is purely URL based, it is less secure.
- As per my observation, not every query operator in LINQ is available in OData like Filter, Skip, Take, etc.
Where is it suitable to use it?
- The case in which performance is more important than security. For example, as I said before it was used in the 2010 Football World Cup scoreboard.
Working of a WCF Data Service
There are five components.
- DataBase.
- Data Access layer using ADO.Net Entity model or LINQ.
- Entity Model Implementing Iqueryable and Iupdatable Interface.
- WCF Data Service exposing CRUD Operations on entity model as REST Service.
- Consuming it on various types of clients.
Supported Message Format
- JSON
- XML (ATOM)
How do I create and implement an OData Service?
Step 1 : Now let us create one Silverlight application with the name OdataDemo.
Step 2 : Add one local db to the OdataDemo.Web application. I have created an OdataDemo database with a table called MyTeam as shown in the following screen.
Step 3 : Add an ADO.net Entity model to a web application so that we can expose MyTable using the Entity Framework.
When you click Add, You will get a screen as shown below. Select Generate from the Database template.
It will ask for a database connection as given below.
So, here the entity name is OdataDemoEntities. After clicking next,
You should select a table; you also can select views if are required. After click finish it will expose the table/database.
Step 4 : Add a WCF Data service to the web application. Here, I have named it "OdataDemoService".
When you add the service, it will create a .SVC and .SVC.cs file. The .cs file will be as shown below.
Here we need to make three main changes to access the service.
1. Enter the DataContext name from the Entity model at the top.
2. Here you can give permission for accessing either read/write/both as shown below.
3. Set the operation rule to all.
That's all! It's done now. Build and browse the service; then you can see the result in the form of ATOM.
You can see metadata using http://localhost:60259/ODataDemoService.svc/$metadata
Now, the service is ready to use. Next we will see how to consume it on the client side.
Step 1: First of all we need to add a service reference to the Silverlight application as given below.
Once you add the service reference, you can access the data context object.
Here, I have taken the service Url and Context object as " ODataDemoEntities ".
Since, Silverlight 4 supports an Asynchronous operation, we should use data service collection or web client. It would be better to use a data service collection, because there is no need to convert it to a list again.
And finally when you run the application, you can see the result as given below.
Wait For NEXT!!
In my next article, I will explain CRUD operations, Custom methods etc.