Over a million developers have joined DZone.

How to Create a WCF REST Service and Host It on Azure

· Cloud Zone

Build fast, scale big with MongoDB Atlas, a hosted service for the leading NoSQL database on AWS. Try it now! Brought to you in partnership with MongoDB.

In this post, we will create a WCF REST service and host it on Windows Azure. We will learn following development aspects of the task:

  • Working with WCF web roles
  • Enabling REST on WCF web roles
  • Returning XML and JSON data from a REST service
  • Deploying WCF service web roles to Windows Azure

First of all, create a project in Visual Studio, choosing WCF Service Web Role from the Cloud tab.


We are going to return list of Players from the Service as XML and JSON. For that add a class to project called Players and attribute it as DataContract. Below I am creating Players class and attributing the class as DataContract and all the properties as DataMember.

public class Players
public string Name { get; set; }
public string Sports { get; set; }
public string Country { get; set; }
public string ImageUrl { get; set; }


After creating DataContract, we need to create ServiceContract. I am creating ServiceContract with two OperationContracts. One OperationContract is retuning XML whereas another is returning JSON. ServiceContract is given below,


public interface IService1

BodyStyle= WebMessageBodyStyle.Bare)]
List<Players> GetPlayersXml();

[WebGet(UriTemplate = "/GetPlayersJson",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
List<Players> GetPlayersJson();


  • There are two operation contracts.
  • GetPlayersXml function will be returning XML as response.
  • GetPlayersJson function will be rturning JSON as response.
  • Using WebGet attribute REST behavior has been enabled on the Operation Contracts.
  • We can get Players as XML at Uri baseaddress/Service1.svc/GetPlayersXml and as JSON at Uri baseaddress/Service1.svc/GetPlayersJson

We have created DataContract and defined the ServiceContract. Next we need to implement service. In implementing I am returning hardcoded data however you can return data from SQL Azure or Azure Storage as well. Service is implemented as below,


public class Service1 : IService1
public List<Players> GetPlayersXml()
return GetPlayers();

public List<Players> GetPlayersJson()
return GetPlayers();


private List<Players> GetPlayers()
List<Players> Players = new List<Players>
new Players
Country ="India", Name="Sachin Tendulkar",Sports ="Cricket", ImageUrl="sachin.jpg"
new Players
Country ="India", Name="MS Dhoni",Sports ="Cricket", ImageUrl="dhoni.jpg"
new Players
Country ="Australia", Name="Rickey Ponting",Sports ="Cricket", ImageUrl="rickey.jpg"
new Players
Country ="India", Name="Sandeep Singh",Sports ="Hockey", ImageUrl="sandeep.jpg"

return Players;

In service implementation I have created a function named GetPlayers(). This function is returning List of Players and calling same function in both function GetPlayerXml and GetPlayerJson.

Last step we need to perform is to enable REST EndPoint on the service. For that we need to configure EndPoint behavior as below,


And then we need to create EndPoint with WebHttpBinding to enable REST on WCF Service. EndPoint will be as below,


In contract PlayerData is namespace and IService1 is ServiceContract we created in previous step. Finally configuration will be as below,

<behavior name="servicebehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
<behavior name="restbehavior">
<service name ="PlayerData.Service1" behaviorConfiguration ="servicebehavior" >
<endpoint name ="RESTEndPoint"
contract ="PlayerData.IService1"
binding ="webHttpBinding"
address =""
behaviorConfiguration ="restbehavior"/>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

This is what all we need to do to create a WCF REST Service to be hosted on Windows Azure. Press F5 to run and test WCF Service Web Role in local development fabric. Make sure that you have set Windows Azure project as startup project.

On running you can see output in browser as below. I am calling GetPlayerXml function


You can see that Players data as XML is accessible at baseaddress/Service1.svc/GetPlayersXml. Since currently we are running WCF Service Web Role in local azure emulator so base address is IP address of localhost at port 81.

Next after testing locally you can deploy service at Windows Azure. For that right click at Windows Azure project and select Package. Next choose Service Configuration as Cloud and Build Configuration as Release and click Package.


After successful package you can see Service Package File and Cloud Service Configuration file in the folder explorer. We need to upload these two files to deploy application on Microsoft Data Center. And later upload this package in Windows Azure Hosted service to host created WCF Service Web Role in Windows Azure.

In another way to publish see this post writen by me to publish from Visual Studio

In this way you can create a WCF REST Service and host in Windows Azure. I hope this post is useful. Thanks for reading.

Now it's easier than ever to get started with MongoDB, the database that allows startups and enterprises alike to rapidly build planet-scale apps. Introducing MongoDB Atlas, the official hosted service for the database on AWS. Try it now! Brought to you in partnership with MongoDB.


Published at DZone with permission of Dhananjay Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}