Windows 8 Consumer Preview is available to Download

image

Folks on the leap day of lap year 2012 are you feeling Metrofied ? Certainly I am , Eventually we have got what we have been waiting since BUILD. Windows 8 Consumer Preview is available to download.

Download ISO image of Windows 8 Consumer Preview from here

clip_image001

Image taken from web.

As they are saying there are more than 10000 changes in consumer preview from the developer preview. Seems there are apps like Kindle, Dictionary etc. are there in the preview.

Download ISO image of Windows 8 Consumer Preview from here

Download Windows 8 and enjoy. I will be doing more developer centric blog post on the same in coming days.

 

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.

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 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.