Building and consuming REST services with ASP.NET Web API using MediaTypeFormatter and OData support

The ASP.NET Web API has been released with the ASP.NET MVC4 beta release, which was released 2 days ago (14/02/2012).

You can download the ASP.NET MVC4 beta release, that includes the Web API, here:
http://www.microsoft.com/download/en/details.aspx?id=28942

Quoted directly from microsoft website:

ASP.NET MVC 4 also includes ASP.NET Web API, a framework for building and consuming HTTP services that can reach a broad range of clients including browsers, phones, and tablets. ASP.NET Web API is great for building services that follow the REST architectural style, plus it supports RPC patterns.

If you do not know what REST stands for and why it could be of any use, you can watch this 1h18m08s video on channel9 by Aaron Skonnard: http://channel9.msdn.com/Blogs/matthijs/Why-REST-by-Aaron-Skonnard

Considering how popular REST is these days, it might be interesting to cover the new Web API in this post. Originally we saw REST services coming up through the WCF pipeline, like the WCF Data Services or using a common WCF service with the WebHttpBinding, which works on HTTP verbs like GET, POST, PUT and DELETE. However WCF was created as a messaging platform, on which we are working with SOAP messages. The entire WCF pipeline is also optimized for messaging. REST services do work a bit differently, nor do they use any SOAP. Apparently Microsoft came to the conclusion that the integration of REST was not ideal with the WCF messaging pipeline so they moved the possibility to create REST services within the ASP.NET Platform.

Continue reading

WCF REST service with ODATA and Entity Framework with client context, custom operations and operation interceptors

It has been a while since I’ve made a post. Real life needed some attention as well and I had to invest into some study into Entity Framework, which is one of the topics I have to master for the upcoming ADO.NET certification. During preparation I found some information on how to construct WCF REST services with ODATA exposing data directly from an entity framework datasource.

For some this might not be interesting, but I sure as hell find this an interesting topic, considering how interoperable and scalable REST services are. In this post I’ll run through the setup and configuration of a  Restfull WCF Data Service with Entity Framework.

I’ve written 2 post before on WCF REST Services, which could be interesting to browse through quickly to understand the basics:
WCF REST service with XML / JSON response format according to Content-Type header
WCF REST service operation with JSON and XML also supporting ATOM syndication feed format

If you are looking for building REST services, this can also be done by the ASP.NET Web API:
Building and consuming REST services with ASP.NET Web API and OData support

Quoted directly from the Open Data Protocol website:

The Open Data Protocol (OData) is a Web protocol for querying and updating data that provides a way to unlock your data and free it from silos that exist in applications today. OData does this by applying and building upon Web technologies such as HTTP, Atom Publishing Protocol(AtomPub) and JSON to provide access to information from a variety of applications, services, and stores. The protocol emerged from experiences implementing AtomPub clients and servers in a variety of products over the past several years.  OData is being used to expose and access information from a variety of sources including, but not limited to, relational databases, file systems, content management systems and traditional Web sites.

OData is consistent with the way the Web works – it makes a deep commitment to URIs for resource identification and commits to an HTTP-based, uniform interface for interacting with those resources (just like the Web).   This commitment to core Web principles allows OData to enable a new level of data integration and interoperability across a broad range of clients, servers, services, and tools.

For this post I used the AdventureWorks database that is provided by Microsoft. You can find it here:
http://msftdbprodsamples.codeplex.com/releases/view/59211 

After attaching the database to your local IIS SQL server, you should have something as following:

Adventureworks Entity Framework

Continue reading

WCF REST service operation with JSON and XML also supporting ATOM syndication feed format

I recently wrote a post about WCF REST services with JSON and XML:

WCF REST service with XML / JSON response format according to Content-Type header

Now I often see ATOM format appearing with REST services, next to JSON and XML (aka POX). However when creating my REST service, I didn’t easily get ATOM integrated. So I played a bit more with it till I got this solution to support ATOM next to JSON and XML on a single WCF REST operation.

First I tried to play a bit with ATOM format as Content-Type, but that thing does not seem to return ATOM results just by Content-Type header on itself. The WebOperationContext.Current.OutgoingResponse.Format only allows the possibility between JSON and XML as well. Somehow I managed to get ATOM results back on the GetEmployees() by the System.ServiceModel.Channels.Message result instead of a List<Employee>, which of course doesn’t allow us to use the exact same method as we used for the JSON or XML format:

WCF REST service ATOM

We get an ATOM feed format back if we invoke this:

WCF REST Service ATOM Feed


Continue reading

WCF REST service with XML / JSON response format according to Content-Type header

Recently I tried to grasp the basics for the WCF REST service and the differences compared to the normal web services versus REST.
I have only tried to basics of WCF REST and not gotten deeper then the shallow level, but this is what I found so far.

If you do not know what REST stands for and why it could be of any use, you can watch this 1h18m08s video on channel9 by Aaron Skonnard:
http://channel9.msdn.com/Blogs/matthijs/Why-REST-by-Aaron-Skonnard 

My solution setup looks like this:

WCF Rest service with XML and JSON

I have a WCF Service Application called “WCF.Rest.Service”, which has a WCF Service called “RestService”.
Our REST WCF service contract looks as following:

WCF REST Service with WebGet and WebInvoke

Notice we added a reference to System.ServiceModel.Web and we are also using this namespace as it is holding the necessary stuff for the REST WCF services.
Instead of using OperationContract we are using WebGet and WebInvoke for REST WCF services.

WebGet stands for retrieval operations and is a HTTP GET operation.
WebInvoke stands for POST requests and stand for the HTTP PUT, HTTP DELETE and HTTP POST operation
Continue reading

By Robbin Cremers Posted in WCF Tagged ,