Over a million developers have joined DZone.

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

· Cloud Zone

Download the Essential Cloud Buyer’s Guide to learn important factors to consider before selecting a provider as well as buying criteria to help you make the best decision for your infrastructure needs, brought to you in partnership with Internap.

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.

image

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.

[DataContract]
public class Players
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Sports { get; set; }
[DataMember]
public string Country { get; set; }
[DataMember]
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,

IService1.cs

[ServiceContract]
public interface IService1
{

[WebGet(UriTemplate="/GetPlayersXml",
RequestFormat=WebMessageFormat.Xml,
ResponseFormat=WebMessageFormat.Xml,
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,

Service1.svc.cs

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,

image

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

image

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

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

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

image

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.

image

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.

The Cloud Zone is brought to you in partnership with Internap. Read Bare-Metal Cloud 101 to learn about bare-metal cloud and how it has emerged as a way to complement virtualized services.

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

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.

SEE AN EXAMPLE
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.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}