Introduction to WCF Data Services
This article walks through creating a WCF Data service [formerly known as ADO.NET Data Services] and consuming from the client application with an attempt to elaborate key technologies and protocols.
WCF Data Services
WCF Data Services is a component of the Microsoft .NET Framework that enables creation and consumption of services that use the Open Data Protocol (OData) to expose and consume data over the Web or intranet by using the semantics of representational state transfer (REST), specifically the standard HTTP verbs of GET, PUT, POST and DELETE. [Ref: WCF Data Services ]
WCF Data Services also includes a set of client libraries. These client libraries provide an object-based programming model to access an OData feed from environments such as the .NET Framework and Silverlight.
WCF Data Services 5.0 release adds support for version 3 of the OData Protocol(OData).
SharePoint 2010 and 2013 utilises WCF Data Services to expose data through list items. Refer WCF Data Services and OData in SharePoint
What is OData ?
Open Data protocol is a standardized protocol for creating and consuming data APIs.
OData builds on core protocols like HTTP and commonly accepted methodologies like REST. The result is a uniform way to expose full features data APIs. [Ref: http://www.odata.org]
OData provides a uniform way to expose, structure, query and manipulate data using REST practices and JSON or Atom Publishing protocol (AtomPub) syntax to describe the payload. It also provides a uniform way to represent metadata about the data, allowing machines to know more about the type system, relationship and structure of the data.
Atom is an XML-based document format that describes lists of related information known as "feeds". Feeds are composed of a number of items, known as "entries", each with an extensible set of attached metadata. For example, each entry has a title.
The primary use case that Atom addresses is the syndication of Web content such as weblogs and news headlines to Web sites as well as directly to user agents. [Ref: The Atom Syndication Format http://www.ietf.org/rfc/rfc4287.txt]
Tools and technologies
- Visual Studio 2010
- Microsoft SQL Server standard or Express
- The Pubs database sample, download here
Implementing the WCF Data Service
Creating a Project in Visual Studio
Choose ASP.NET Empty Web Application template from New Project window in Visual studio as shown below.
Add ADO.NET Entity Data Model to the Data folder in the project as shown below.
In the Entity Data Model wizard choose 'Generate from database', New connection for Pubs database on the server, keep the check box 'Save entity connection settings in Web.config as', in the 'Choose your database objects window select the employee table under Tables, keep the selected check boxes for Pluralize or singularize generated object names and Include foreign key columns in the model. Selecting the Finish button finishes the wizard and generates an Entity framework Object relational data model.
Refer the video How Do I Get Started with the EDM Wizard?
Above concludes the Data folder creation process.
Add WCF Data Service template
Add a folder named as Service to the project, choose Add New Item and select the WCF Data Service template under Web category as shown below.
After adding the above WCF Data Service code behind opens to implement the service as shown below.
DataService class takes a generic type as parameter that is exposed through the service.
Replace < /* TODO: put your data source class name here */ > with the PubsEntities
As the below comment says uncomment the entity set rules and/or add few more entity set rules to expose the entity sets or service operations through web service.
// TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
is for adding access rules to database objects such as Stored procedures, Functions and Views etc.
Note the various access rights can be set on entity set as shown below .
That is it! Web Service is ready to expose the data.
Above concludes the WCF Data Service creation process.
Lets view the service
Select the View in Browser from context menu of PubsDataService.svc , which displays the service with the href and the collection title etc as shown below.
Appending $metadata to the URI renders the metadata for the service as shown below.
Appending resource href to the base service URI renders the data in Atom feed. Note that it is required to have a browser add-on such as ‘Feed Intent Viewer’ to view the feed. Below screen shot shows the feed from the service that is rendered in the Feed Intent Viewer [Chrome add-on].
Selecting the Feed Intent Viewer button displays service data in Atom format as shown below.
Being REST service any tool that can send Http requests invokes the service. Typical tools are web browsers, Fiddler (Web Debugging tool), web browser add-ons such as Poster for Firebox, REST Client for Chrome etc.
Consuming the WCF Data Service in Client application
Add a ASP.NET web form to the Client folder of the project, then add GridView control to the web form to display the service data.
Add Service Reference
In order to consume the service in Web Form application add the service reference to the project.
Select ‘Add Service Reference’ context menu item from the context menu from the project. As the web service is in the same solution UDDI (Universal Description Discovery and Integration) can locate the service. Refer UDDI.ORG for more information.
Refer How to: add a Data Service reference
Selecting the Discover button in the Add Service Reference window utilises UDDI, access the service, displays the URI and the entities that are exposed through the service as shown in below screen capture.
Note that for remote web services it is required to enter the URI and select Go button to view the service with available operations and entities.
Amend the ServiceReference to the desired one and select OK. Visual Studio generates a proxy class on client side to interact with the data service. These proxy classes and resources can be seen by selecting the Show All Files icon just below the solution explorer menu. Below screen capture shows the proxy class and resources that are generated by visual studio.
Note that Visual Studio utilises DataSvcUtil.exe to generate the proxy classes in the solution. DataSvcUtil.exe is a command-line tool provided by WCF Data Services that consumes an Open Data Protocol (OData) feed and generates the client data service classes that are needed to access a data service from a .NET Framework client application. [Ref: WCF Data Service Client Utility]
DataSvcUtil.exe can be utilised to generate proxy classes directly from command line Refer WCF Data Service Client Utility.
Invoking the data service and consuming the data
In the web form code behind create a context, define the query and bind the service response to the control as shown in below source code.
1: protected void Page_Load(object sender, EventArgs e)
3: // Create the DataServiceContext using the service URI.
4: var context = new ODataService.PubServiceReference.pubsEntities(new Uri("http://localhost:64724/Service/PubsDataService.svc"));
7: //Define a query to access the employees
8: var query = context.employees.ToList();
10: //Bind data to the Gridview control
11: GridView1.DataSource = query;
14: catch (Exception ex)
Build the project to resolve any exceptions. That is it!
Client application can invoke the WCF Data Service and receive the data. Note that it is required to set Data Service as ‘Set As Startup Page’. At runtime data service is up and listening to client requests. In this example client is the web form. In this article both service and client applications are within one project, it is suggested to host the Data Service as separate application in IIS to be consumed by multiple client applications.
Navigating to the employees.aspx page shows the GridView with the service response data as shown in below screen capture.
Conclusion: - OData Service allows Linq queries in the Uri to access the data (resources) easily. Refer OData Query Builder for further information. As this article introduces WCF Data Services, advanced queries or concepts are discussed in another article.
Refer www.odata.org for the documentation, libraries, API explorer and more resources.
The completed Visual Studio solution with the project is shown below.
WCF Data Services Overview
WCF Data Services on MSDN
WCF Data Services MSDN video tutorial
OData with Scott Hanselman
Data Client (WCF Data Services)
OData Query Builder
WCF Data Services and OData in SharePoint 2010