Over a million developers have joined DZone.

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

DZone's Guide to

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

· Cloud Zone ·
Free Resource

Learn how to migrate and modernize stateless applications and run them in a Kubernetes cluster.

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.

Join us in exploring application and infrastructure changes required for running scalable, observable, and portable apps on Kubernetes.

dotnet ,cloud ,how-to ,tools ,service ,rest ,wcf ,azure

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}