Channels ▼
RSS

.NET

Exposing RESTful Services Using WCF


Joydip Kanjilal is a Microsoft MVP in ASP.NET and author of numerous books on Windows programming. He can be contacted at joydipkanjilal@yahoo.com.

More Insights

White Papers

More >>

Reports

More >>

Webcasts

More >>


REST, short for "Representational State Transfer," is an architecture paradigm for creating scalable services. A RESTful Web Service is one that conforms to the REST architecture constraints. Microsoft's Windows Communication Foundation (WCF) is a service-oriented framework that can be used to expose a RESTful service. A RESTful Web Service exposes resources URIs, then uses the HTTP methods to perform CRUD operations. In this article, I examine the basic principles of REST, explain what a RESTful service is, and show how a RESTful Service can be exposed using Windows Communication Foundation.

The code examples I present here and the downloadable source code that accompanies this article are implemented using Visual Studio 2010 and C# 4.0. To interact with the code examples in this article, you need to have Visual Studio 2010 installed in your system.

Representational State Transfer (REST)

Again, REST is an architectural paradigm in which an application's state and functionality is divided into resources which in turn is addressable using URIs over HTTP. A REST-based model is stateless, client-server based, cacheable and uses resources that have a common interface and are uniquely addressable.

Resources in a REST-based mode model are used to represent state and functionality and identified using logical URLs. Although REST is based on the stateless HTTP protocol, resources are cacheable -- you can also set expiration policies for your cached data. In a typical REST-based model, the client and the server communicates using requests and responses -- the client send a request to the server for a resource which in turn sends the response back to the client.

A request in a REST-based model contains an Endpoint URL, a Developer ID, Parameters and the Desired Action. The Endpoint URL contains the complete address of the script. The Developer ID is a key which identifies each request origin and is sent with each request. You can pass Parameters to a REST request just as you do with your method calls in any programming language. The Desired Action in a REST request is used to denote the action to be performed for the particular request.

Here's what a typical REST request URL looks like:


http://localhost/sales?devkey=17&action=search&type=product&keyword=ProductID 

In the above request, the endpoint is http://localhost/sales, the action desired is search, and the developer key is 17. What follows next in the above URL is the type and keyword parameters.

An example SOAP request is shown below:


<?xml version = "1.0"?>
<soap:Envelope>
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
 <soap:body s="http://localhost/sales">
  <s:GetSalesDetails>
   <s:ProductID>1</s:ProductID>
  </s: GetSalesDetails >
 </soap:Body>
</soap:Envelope>

The following code snippet illustrates how the above SOAP request can be represented using REST:


http://localhost/sales/SalesDetails/17


Related Reading






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video