Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Step by Step Approach to Create a WCF Data Service

0.00/5 (No votes)
31 Jul 2015CPOL5 min read 9.9K   17  
Step by step approach to create a WCF Data Service

When it 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 deeper into the definition of what 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 the 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 exposes 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 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 help you in building a Model.

    ADO.NET Entity Data Model

    Model Contents

    ADO.NET Data Connection

    Choose Database Objects

  • Now we are done with Entity Model and for our Schema structure, this how the Model looks:

    Entity 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 an 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. For example, if we need a Single record, access it like below:

    OData - WCF Tutorial

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 Gridview.

    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 in understanding more about Web API service, follow Web API tutorial.

Retrieve the Data in JSON Format

JSON format is quite easy to read programmatically and faster than XML. In-order to get the data in JSON format, we need to have a 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 a 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

The post Step by Step approach to create a WCF Data Service appeared first on WCF Tutorial.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)