Monthly Archives: February 2012

Step 3 – Creating the WCF Data Service

This post is Step 3 in a Quick Start tutorial on how to create WCF Data Services

Now for the fun bit, exposing our data using WCF Data Services.

1. Start by creating a new project in the solution using the template – ASP.NET Empty Web Application calling it something like WcfDataService.

2. Add a reference to the data model project we created in step 2.

3. Copy the ConnectionStrings section from the App.Config file in the data model project into the Web.config file of the WcfDataService project that we have just created.

4. Now add a new file file (Add New Item) to the Data Service Project of type ‘WCF Data Service’

WCF Data Service Template

In the Code view of the service we have just added (wcDataService1.svc.cs by default) we need to make the following code modifications:

Modifing the Data Service Class

The first modification lets the service know which data we want to expose, in this case the entities representing our model created in step 2 via the Entity Framework.

The second modifications are relating to security.  This is quite a cool feature as it allows us to specify which parts of our data should be available via the service and to what degree that data should be available e.g. Read only, Read & write etc.

Ok, now to run out service (yep thats it, not much coding to get a service initially set up!!)

Right click the Service in the project and select ‘View In Browser’:

If your service has been created correctly you should see an XML representation of the entity objects we created in step 1 and 2 and exposed via the service earlier in this step.

Results from calling the WCF service

So we have a fully functional WCF Service but it doesn’t really show a lot at the moment.  The next Step in this blog post gives more of an initial insight into the data that is being served up by the service and how to manipulate it.

Step 4 – Understanding the Results


Step 2 – WCF Data Services – Entity Framework Model

This post is Step 2 in a Quick Start tutorial on how to create WCF Data Services

Using the Entity Framework is a great way to model the database and serve up our data although if you are using MySQL as your data source like me, you will need to install the connector for .NET which will allow Entity Framework to import from a MySQL.  The Connector can be downloaded here

1. Once the connector is installed open Visual studio ‘create a new Project’

2. Within the Project click to Add a New Item – ADO.NET Entity Data Model and name it ‘companyModel’

3. When the Entity Data Model Wizard appears click ‘Generate from database’

4. On the Connection screen hit ‘New Connection’ and on the following Connection Properties screen you should be able to select the MySQL driver (if the connector successfully installed) and complete your credentials for the database:

Connection Properties for the MySQL Data Source

5. Select to import all the tables we created in step 1 and click finish to generate the model and if all goes correctly you should end up with the data structure created in step 1, imported into the Entity Framework

Entity Framework model representing the MySQL Data Structure

Follow the link for Step 3 where we expose this data using a WCF Data Service

Step 3 – The WCF Data Service


Step 1 – WCF Data Services – The MySQL Database

This post is Step 1 in a Quick Start tutorial on how to create WCF Data Services

First just a bit of background.  I am going to create a simple database structure in MySQL that has a few tables but also enforces relationships.  As I’m not doing a MySQL tutorial I will not go into the technicalities of creating the database but if you feel more confident with something else e.g. SQL Server then just copy the following structure in your database of choice.

We are going to model the following scenario:

In our organisation there is a number of staff and a member of staff may care for one or more supported people

The database structure I have created to represent this scenario looks like this (Note: I have used MySQL workbench to create the following EER model – Once created you can use the Database à Synchronize model feature of workbench to port the structure straight into your database)

The sample Data Structure that will be exposed using WCF Data Services

Once the database has been created populate the tables with some sample data e.g.

Populate with sample data

Ok so we have a database to follow the link to Step 2 where we import this data into the Entity Framework

Step 2 – The Entity Framework


WCF Data Services – Quick Start

One idea I’m quite passionate about is REST style architectures.  It seems to make perfect sense to keep all the data logic self-contained and enforce the point that the client must make requests for data and return appropriate responses without being highly coupled on the server.

A technology that allows us to do this quick and easily and enforce a level of security over access to the data is WCF Data Services – Although keep your eye on the idea if Web APIs available with MVC 4

One thing I found when studying WCF Data services is that it opens up a whole jar of other technologies and ideas e.g. Entity Framework, OData etc.   Therefore for this post I am just going to demonstrate how to create a simple MySQL database, import it into the Entity Framework and expose the data using WCF Data Services so that you could consume it with any client you see fit.

So you are not overloaded with one big blog page I have broken this tutorial down into separate posts:

Step 1 – Creating the MySQL Database

Step 2 – The Entity Framework Model

Step 3 – The WCF Data Service

Step 4 – Querying the WCF Data Service