How to create WCF REST Service and host in Windows Azure

In this post, we will create a WCF REST Service and host in Windows Azure. We will learn following things in this post

  • Working with WCF Web Role
  • Enabling REST on WCF Web Role
  • Returning XML and JSON from REST service
  • Deploying WCF Service Web Role to Windows Azure

We need to host WCF Service in Windows Azure. So create project choosing WCF Service Web Role from 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 wriiten 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.

8 thoughts on “How to create WCF REST Service and host in Windows Azure

  1. I tried to create a webservice in the way you described. It works perfekt in the browser… but i could not create a WindowsApplication that consumes the Service. The app.config-File is always empty, and adding a ServiceReference to the WindowsApplication-Project does not generate any code in app.config file.

    Can you help me?

  2. Thank you very much – this article was a huge help in helping me setup a restful API in Azure.
    I initially was trying to set this up in a MVCWebRole but it wasnt working. Only when I switched to WCFServiceWebRole did it work.
    It seems to be that some of the web,config in the MVCWebRole was breaking it. Do you know why the following web.config prevents the above code from working??


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 )

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s