Step by Step approach to create a WCF Data Service

By | April 20, 2015

When comes to Service Oriented Architecture or Programming, Microsoft’s WCF (Windows Communication Foundation) technology plays a prominent role in the field. It enables developers to segregate Services/Data with Client, hence it helps in developing a loosely coupled application with ease.

Before digging into further definition of what is WCF Data Service is, let me clarify the common confusion between WCF Service and WCF Data Service. In WCF Service, the focus is on creating Operations, where the generic services are implemented. It works with various protocols/techniques such as SOAP, MSMQ, HTTP, TCP etc. Whereas WCF Data Service is completely focused on creating Data as a service. If we have a data model and if we need to make it available for the clients to consume it, then WCF Data Service is the right way. It only supports REST based HTTP protocol.

Here in this WCF tutorial, we are going to follow a simple step by step approach to create and consume a WCF Data Service. You can also download complete source code here.

WCF Data Services 4.5 exposed

Microsoft’s .NET framework provides us a reliable Service technology called WCF Data Service, where it enables us to create a Data service which expose our data-model to the clients. Here the services, which it creates are based on OData i.e. Open Data Protocol, it is a standard technique to create and consume REST (Representational State Transfer) APIs, where the services are accessed by URLs via normal HTTP requests.WCF Data Services

HTTP request includes GET, POST, PUT and DELETE verbs through which a CRUD based service can be created. This Data service can be consumed by any client application that supports HTTP-OData and format of receiving data can be of XML, JSON or Raw text.

WCF Data Services Tutorial – A Step by Step Approach

Create a WCF Data Service

You need Visual Studio 2010 IDE & SQL Server in your machine as prerequisites for building WCF Data Service. It would be nice if you have the basic .NET knowledge. Let’s get started!

    • Open Visual studio 2010 and create a Web -> ASP.NET Web Application with a language of your choice, either C# or VB. I’ve used C# throughout the lesson. The reason why we are creating an ASP.NET Web application is because in order to host and consume the WCF Data Service we need a hosting and client application.WCF Tutorial - Step 1
    • Once the application is created, we need a DataModel where it is going to be served. So let’s create a SQL Server database. Right click on the App_Data -> Create SQL Server database and define the schema as below.

WCF Tutorial - Step 2

  • Now let’s feed some values into our newly created database table.WCF Data Services - Step 3
  • We have our Database and Table ready, now it’s time for us to create an Entity Data Model. Right click on the project and Add New item -> Select ADO.NET Entity Data Model, a Wizard pops up, proceed further with it. Below screenshots helps you building a Model.ADO.NET Entity Data ModelModel ContentsADO.NET Data ConnectionChoose Database Objects
  • Now we are done with Entity Model and for our Schema structure, this how the Model looksEntity Data Model
  • We are good with our basic implementation in-order to adopt a WCF Data Service into our application. Ready to go. Let’s create a WCF Data Service. Right click on the Project -> Add New Item -> Select WCF Data Service.WCF Tutorial - Data Service
  • WCF creates us a basic template code for us to start with.WCF Tutorial Template Code
  • Before moving forward, let us keep our Database Entity name handy. It can be found in Web.Config under Connection String.Database Connection String
  • Here we have replaced the template code with EntityName and Set of rules which decides the rights of access.DB Entities WCF Tutorial
  • If you run the application or just hit F5 selecting the WCF Data Service file. It opens up a browser and navigates us to http://localhost:/WCFDataFilename.svc . You can see a XML document with title holds to the Entity Name we created.WCF Data Services File
  • To get the full database values as a XML model, just postfix the Entity Name with URL. As below.WCF Service XML Model
  • Quite simple right! We do have some syntax approach in accessing the resource via OData. Example if we need a Single record, access it like belowOData - WCF Tutorial

If you are interested in getting more information about data visualization, you can follow here for online tableau training.

Consume Data Model via WCF Data Service

  • Open Default.aspx page of the ASP.NET Web application that we created a moment ago. Create a Grid View.ASP.NET Web Application
  • In-order to consume the WCF Data Service, we need to add a Service Reference. Right click on Reference Folder -> Add Service Reference -> Click on the Go. Since our Data service resides inside our application it fetches URL automatically.Add Service Reference
  • Create an instance for the referenced Service, place the URI of our running Data Service. Map the Entity with the DataSource of the GridView.Running Data Services
  • Just run the application or hit F5 to see the Magic!WCF Data Service
  • Great! We have successfully created a WCF Data Service and Consumed it. If you are interested to understand more about Web API service, follow Web API tutorial.

Retrieve the data in JSON Format

JSON format is quite easier to read programmatically and faster than XML. In-order to get the data in JSON format, we need to have special class file “JSONPSupportBehaviour.cs”. Include the file into our project and just decorate the Data Service class with the attribute JSONSupportBehaviourAttribute.

That’s it, now we can just retrieve the data in JSON format, by following OData special URL format i.e post fixing with $format=json.

You can get the JSONPSupportBehaviour.cs by downloading the Source code.JSONP Support Behavior

WCF Tutorial

WCF Tutorial

Hope this article would have sown little knowledge on WCF Data Service. Kindly let us know if you have any questions.

Download Source Code for this WCF Tutorial

Enjoy WCF Programming with WCF Tutorial!

Top 10 Interview Questions and Answers Series:

Category: WCF Data Services Tags:

About IMRAN ABDUL GHANI

Imran Abdul Ghani is working as Software Developer(Senior) with extensive knowledge in Web development technologies especially C#, ASP.NET, MVC, WCF, Web API, ADO.NET Entity Framework, jQuery etc. He has several years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET (MCSD.NET) since 2005. You can reach his blogging at www.webdevelopmenthelp.net, www.topwcftutorials.net, and www.sharepointfordummies.net.