Channels ▼


Exposing RESTful Services Using WCF

Windows Communication Foundation (WCF)

More Insights

White Papers

More >>


More >>


More >>

The Windows Communication Foundation (WCF) was introduced as part of .NET Framework 3.0. WCF is a Microsoft framework that you can use to implement connected, service-oriented, reliable, transacted services that are reliable and secure. In WCF you have a framework that provides an excellent unification of Microsoft's distributing technologies (Web Services, Remoting, COM+, and so on) under a single umbrella.

The three main concepts related to WCF are:

  • Address
  • Binding
  • Contract

While address denotes the location of the service, binding specifies the communication protocol and the security mechanisms that apply. A contract defines the parts of the service that is exposed. In WCF, a client connects to a service using endpoints exposed by the service. An endpoint contains the address that denotes where the endpoint can be accessed and the binding information for the endpoint. When a client needs to connect to a service and execute one or more of its exposed methods, it should adhere to the binding specified by the endpoint.

RESTful Web Services

A RESTful Web Service (or the RESTful Web API) is a service that comprises of a collection of resources. These resources include a base URI that is used to access the web service, a MIME type (i.e., JSON, XML, etc) and a set of defined operations (i.e., POST, GET, PUT or DELETE).

It should be noted that similar to a Web Service, a RESTful Service is platform and language neutral. However, unlike a Web Service, there isn't any official standard set for RESTful Services -- it's simply an architectural style.

Implementing a RESTful Service using WCF

To implement a RESTful Service using WCF, you start by using Visual Studio 2010 to create a WCF service and then make the service RESTful using the necessary attributes. To give you an idea of what the Solution Explorer would look like when implementing the Service, Figure 1 shows the completed Service Host and the Service Client.

[Click image to view at full size]
Figure 1: The Solution Explorer with the RESTful WCF Service, Service Host, and Service Client.

Creating the WCF Service

A WCF service is comprised of the following:

  • Service class
  • Service contract
  • Hosting environment
  • One or more endpoints

The Service class in WCF implements a Service contract. The Service contract is in turn decorated using the [ServiceContract] attribute. A WCF Service needs to be hosted in a hosting environment--you can have your WCF Service hosted in IIS, or make it a self-hosted service. The hosting environment denotes the environment in which WCF Service executes. WCF Clients use the endpoints to access the WCF Service. A WCF Service contains one or more methods that are exposed to the outer world using the [OperationContract] attribute.

To create a WCF service using Visual Studio 2010, follow these steps:

  1. Open Visual Studio 2010 IDE.
  2. Click on File -> New -> Project.
  3. Select WCF Service Application from the list of the project templates displayed
  4. Specify a name for the WCF Service Application and click OK to save.

This would create a WCF Service Application project. Here's what the Service class looks like:

using System;
namespace DDJService
    public class Service1 : IService1
        public string GetData(int value)
        public CompositeType GetDataUsingDataContract(CompositeType composite)
            if (composite == null)
                throw new ArgumentNullException("composite");
            if (composite.BoolValue)
                composite.StringValue += "Suffix";
            return composite;

A Service class in WCF implements a Service Contract -- an interface that contains the [ServiceContract] attribute. Here's what this interface looks like:

using System.Runtime.Serialization;
using System.ServiceModel;
namespace DDJService
    public interface IService1
        string GetData(int value);
        CompositeType GetDataUsingDataContract(CompositeType composite);
    public class CompositeType
        bool boolValue = true;
        string stringValue = "Hello ";
        public bool BoolValue
            get { return boolValue; }
            set { boolValue = value; }
        public string StringValue
            get { return stringValue; }
            set { stringValue = value; }

Implementing the Service Contract

I will now implement a WCF Service Contract called "TestService" for our RESTful Web Service example. I'll make the service RESTful later; for now, I focus on creating a Service Contract.

So what is a Service Contract, anyway? A Service Contract is an interface that is marked with the [ServiceContract] attribute and contains declaration for one or more methods that are exposed using the [OperationContract] attribute. The Service Contract will have two methods -- GetEmployee and PostEmployee. Here's he Service Contract:

    public interface ITestService
        Employee GetEmployee(Int32 employeeID);
        Employee PostEmployee(Int32 employeeID);

The GetEmployee and PostEmployee methods return instances of the Employee class. The Employee class is a custom class -- a complex type that is marked with the [DataContract] attribute so that instances of this class can be serialized. Note that you can only transfer instances that are serialized -- the primitive types in .NET are all serialized by default. The Employee class is shown below:

[DataContract(Namespace = "")]
    public class Employee
        public Int32 EmployeeID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public String Address { get; set; }

Related Reading

Currently we allow the following HTML tags in comments:

Single tags


<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags


<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

