Patrick Desjardins Blog
Patrick Desjardins picture from a conference

WCF Data Services is the implementation of OData by Microsoft

Posted on: 2013-07-18

Several time a week I hear confusion concerning WCF services, WCF Data Services, ADO Data Service and OData. First of all, ADO Data Service is out. In fact, it's the old name for WCF Data Service which is the good sentence for OData by Microsoft. WCF service focus on contract when Data Service focus more on the data. OData is been invented by Microsoft but is not the implementation in .Net. It's the protocol which is open.

WCF Data Service is a type of service that provide information through url. This is easily consumable because it requires only a web browser to execute a call which can have filter, join between entities and conditions. WCF Data Service uses the entity-relationship conventions of the Entity Data Model to expose resources as sets of entities that are related by associations. Not only can you get information the way you want but you can also update entity and insert.

WCF Data Service lets you use two well know structured semantic which is Atom (XML) and JSON. Both of these protocole are RestFul. That mean that when a call is executed, the life cycle end once the data is sent back to the consumer.

To create a WCF Data Service, simply create a class that inherit from DataService. It's a generic class so you will have to specify a DataContext type. To use this class, you need to reference the dll : System.Data.Services.

public partial class MyService: DataService<MyDbContext> { 
  //... 
} 

You will have to also specify which entities is allowed to be accessed in InitializeService.

public static void InitializeService(DataServiceConfiguration config) { 
  //... 
} 

The simplicity lets you add this service in a later stage of your project. It's even more true if your project is already using Entity Framework because OData is tightly coupled Entity Framework way to handle entity. This is why it's very easy to create a WCF Data Service with Entity Framework because of the Data Model that both share.

OData is between the consumer and the source, the consumer could be a simple web browser and the source a .Net application or SQL Azure server. This is very powerful because you can make a mobile application that use the same service as a web site or a winform. Even more, you can create on different web browser (Internet Explorer, FireFox, Chrome) or different mobile platform (Android, IPhone, Windows Phone) with the same WCF Data Service.

The OData protocol lets you send more than only GET request. GET http request will only give you the possibility to receive data. In the case you want to delete data, a DELETE http request would be required. POST is used to insert, PUT to update. The protocol uses string and not a reference, this is why XML and JSON is used. It also respect the concept of not being tight to the server by being RestFul. So in short, a SQL table (rows) is converted into Entity Set (entity) which is converted to a collection of entry (entry).

WCF Data Services also supports data models that are based on common language runtime (CLR) objects that return an instance of the IQueryable interface.