Using WCF Web API in .NET 4.5 and WCF 4.5

In previous posts we have seen some of the new features coming in the next version of .NET 4.5 and WCF 4.5. Today we are going to talk about new tool which simplifies the process of exposing data, services and application through the HTTP. This project is Codeplex project named as WCF Web API. Developing as an open source project, Microsoft want that developer and potential user can access and be part of the development process from the early stage to the final releases and integration to next version of .NET and WCF. Detailed information about WCF Web API you can find on site. As we mentioned with WCF Web API you dramatically simplifies the process of exposing data through service to consumers.

This blog post will describe how to use WCF Web API and to show how you can simply convert you existing WCF Data Service in to the service based on the WCF Web API, in just a few lines of code.

The most important features WCF Web API contains are:

-First-class programming model for HTTP in WCF
-Access HTTP from top to bottom
-Content negotiation
-Out of the box support for XML, JSON and OData
-Typeless JSON support – Allows using a dynamic object on the server for accessing the body. Useful for jQuery clients.
-Plug in any format / media type
-IQueryable support – Supports OData’s URI format for services
-Designed for better separation of concerns and testability
-Validation, URI model binding, caching, etags, etc
-Easily integrate your IoC container
-Simple code-based configuration model
-Support for HTTP file upload and MIME-based batching
-Integrated Web API test client

Note that WCF Web API is working on the .NET 4.0 and Visual Studio 2010 as well, but with this post we want to show how such a technology works on the next version of .NET 4.5 and WCF 4.5. In fact, we will see how simply we can implement data service and expose it to ASP.NET application, or any client which can make request to the service in just few lines of code. To see simplicity of the WCF Web API, we will use existing WCF Data Service and convert it to the new version based on the WCF Web API. The existing project is demo developed for the last DevDays conference held in Sarajevo. The demo sample we are going to use here, you can download from this link. After you download the zip file, open it and you will get the content as next picture show.

The BugTracked Demo sample contains three folders:

-SQL Database –data tier and sql database for the project
-BugTrackerWCFClients – folder contains clients using the service
-BugTrackerService – folder contains WCF Service and EF DBModel project.

&For this demo we will use only DBModel. This is EntityFramework project which maps the tables from SQL database.

Now Open the Visual Studio 11 DP and create new ASP.NET empty application. Name it WCFWebAPISample, and click OK, similar as picture below.

Now that we have project we are going to use NuGet to install WCFWebAPI directly in to the project. Right Click on project and choose: Manage NuGet Package.

After WCF Web API installation, we need to copy existing EF project in to WcfWebAPISample solution folder and import it to the current WcfWebAPI Solution.

First Copy the folder in to Current solution folder:

If you haven’t installed NuGet you can install it from the following link. It also supports VS 11 beta, when the beta appear. Type WCF Web API in to search box and click on Install button of the WebApi.All.

After WCF Web API installation, we need to copy existing EF project in to WcfWebAPISample solution folder and import it to the current WcfWebAPI Solution.

First Copy the folder in to Current solution folder:

Return to VS 11 and Add existing project to current solution. After importing the project add the following reference to WCFWebAPISample project:

Note: Be sure that recently imported project is set to use .NET 4.5 instead of old .NET 4.0.

After we complete the importing BugTracker.DBModel project we need to setup database. Copy btdb database to you database folder and attach it to the SQL Server, or you can use Model First feature of the Entity Framework and create database to your SQL Server.
If you want to use Model First Feature connect to SQL Server as picture shows.

Create database and name it as BTDB, like the folowing picture:

Open BugTrackerEFDBmodel.edmx, right click and choose: Generate Database From Model…

Click Finish on the Wizard and complete SQL Script is generated for run.

Execute the script and the BTDB is created on SQL Server. Before we use it, wee need to fill some data. So that open every table and put at least one record in it, or just copy the data from the downloaded database.

Now the we have the Database and DBModel we can build our service to get manipulate data. If you look the existing project , we have created the WCF service project, implement Service interface, and service implementation. We have also implement Data Contract and define which data type can be exchanged thorough the service. We have also implement REST API for each Service operation, to be able to retrieve data by REST API. This was pretty big job, in comparison to what we are going to implement. To use our DBModel with WCF Web API, we need to implement one class, with BugTrackerWebAPI name.

The following code represent implementation for the class:

The class is decorated with [ServiceContract], and operation with [WebGet], attribute and this is all we need to implement. This two attribute replace whole implementation of WCF Service. The last thing wee need to implement before we runt the app is route registration in to Global.asax.

Add new item, choose Global.asax, and write just only one line of code, showed in the following picture:

We only register Api/bug route to Service implementation. This rout we need to put in to project property to be able to run the service immediately when browser appears. The picture below show this step.

After we run the application browser shows our data in XML format:

As we have seen this is very easy implementation of the Service data, without any configuration setting, DataContract and other WCF specific feature. On this way WCF becomes very hady tool in lighter application architecture, which was not the case in previous versions.

The complete implementation of this Demo you can download from here.


2 thoughts on “Using WCF Web API in .NET 4.5 and WCF 4.5

  1. Pingback: The future of Windows 8 desktop application « Bahrudin Hrnjica Blog

  2. Pingback: Using WCF Web API in .NET 4.5 and WCF 4.5 - C# & .NET technologies -

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s