WCF Data Services: Overview

Description Article
Description

What is it

WCF Data Services its a technology based on WCF and it’s main goal is to achieve creation of data services from any data source, structuring data as entities/resources as specified on an entity model.

Data source data is then consumed through:

  • An URI executed in a common browser as a ReST HTTP GET request;
  • A .NET Client application where query execution is easily enabled through LINQ: This LINQ queries will be translated into the correspondent URI.

For query purposals the entity resources must implement the IQueryable interface.

For update purposals the entity resources must implement the IUpdatable interface.

Protocols

The Open Data protocol (OData) is the underneath protocol used and which allows to develop web-services following a Resource Oriented Architecture (ROA) instead of a Service Oriented Architecture (SOA).

Accessing Data Service Resources

The URI plays the main role and represents the way how the data service resources are addressable for consume by Client applications or the web browser. It presents the following structure:

http://localhost/TraningService1.svc/Category(1)/Products?$top=2&$orderby=name
__________________________________/ ____________________/ ___________________/
    |                                 |                   |
service root                       URI resource path     query options

Addressing samples

Returns a feed that is the Customers entity set, which contains entries for all instances of the Customer entity type:

http://localhost/Training/TrainingService1.svc/Customers

Returns an entry for a specific instance of the Customer entity type that has a key value of “João”:

http://localhost/Training/TrainingService1.svc/Customers('João')

Returns an XML element that contains the ContactName property value for a specific Customer:

http://localhost/Training/TrainingService1.svc/Customers('João')/ContactName

Returns that contains the ContactName property value for a specific Customer without the XML element:

http://localhost/Training/TrainingService1.svc/Customers('João')/ContactName/$value

Returns a feed that is the set of all the Orders that are related to a specific Customer:

http://localhost/Training/TrainingService1.svc/Orders(10643)/Customer

Return a feed with the Orders filtered for the specified Customer that have shipped since January 15, 2013:

http://localhost/Training/TrainingService1.svc/Customers('João')/Orders?$filter=ShippedDate gt datetime'2013-01-15T00:00:00'

Query options can be added for filtering, sorting, paging and projections. Refer to OData: URI Conventions for details.

Message format and contents

The HTTP PUT and POST request message and the HTPP responses (except for HTTP DEL request) contains the entity data sent to the data service. The contents depends on the data format of the message.

The supported message format are:

  • Atom – An XML-based message encoding that is defined by OData as an extension to the Atom Publishing Protocol (AtomPub) to enable data exchange over HTTP for Web feeds
  • JSON – JavaScript Object Notation (JSON)

The message format is in the header of the HTTP request message, namelly:

  • application/json; or
  • application/atom+xml,application/atomsvc+xml,application/xml

Quering Results Samples

All the exposed entites:

Querying for a specific customer:

Querying for all orders of a specific customer:

When to use

  • Multiple front-ends must be supported like mobile devices;
  • Data needs to be exposed to third parties that will submit query requests as needed (filters and related data allowed);
  • Data is to accessible from a browser only (for whatever reason);
  • Support for Silverlight is planned

When not to use

  • Business logic is required;
  • Data types not present in the data model are needed;
  • Sub-queries are fully used, sice they are not supported

Providers

WCF Data Services supports multiple provider models for exposing data as an Open Data Protocol (OData) feed.

The following providers for defining the data model of a data service:

Provider Description Remarks
Entity Framework provider This provider uses the ADO.NET Entity Framework to enable the use of relational data with a data service by defining a data model that maps to relational data. The data source can be SQL Server or any other data source with third-party provider support for the Entity Framework It implements the IUpdatable  interface for CUD operations.The native implementation can not be customized, extended or replaced for providing additional business logic functionality in the data service
Reflection provider This provider uses reflection to define a data model based on existing data classes that can be exposed as instances of the IQueryable interface. Updates are enabled by implementing the IUpdatable interface The implementation of data services using Linq to SQL or NHibernate  makes use of the reflection provider. Any class that implements the IEnumerable interface can use the “AsQueryable” method and return an IQueryable interface. This way any in-memory generic collection can be used as data source for the data service
Custom providers This is a set of providers that enable the possibility to dynamically define a data model based on late-bound data types Includes interfaces for implementation when the data being exposed is not known when the application is being designed or when the Entity Framework or reflection providers are not sufficient

Hosting Environment

Since WCF Data Services is designed to integrate with Windows Communication Foundation, the data service can be hosted in the same way and where the comon WCF service can be hosted.

Refer to this article for WCF hosting details.

Who can use it

Data services can be consumed by any type of client application: Windows, Web, SilverLight, AJAX and console type applications.

.NET-based Client applications can use LINQ to access data and a simple object layer to update data.

 

 

Help

Refer to these articles for detailed description of WCF Data Service Development

Refer to these articles for detailed description of Service Consumption using a WCF Client application

References

[1] Open Data Protocol explanation through examples article
[2] OData operations article
[3] Refer to this article for description of how to debug a WCF Data Service