TechBubbles Microsoft Technology BLOG

WCF Services in Windows Azure

 

What is Windows Azure? Windows Azure provides developers computation power storage and can scale web applications on internet through Microsoft Datacenters. Developers can use Azure platform to build WCF service application using their existing Visual Studio 2010 expertise. image

Windows Azure supports the protocols and formats including SOAP, REST and XML. This post discuss about creating a simple WCF Service and hosting it on Windows Azure and consuming in client application.

1. Create a WCF Service Contract in Windows Class Library project as shown below which in-turn you use in Web role, worker role and in client application. Read more about roles in windows Azure here

   1: using System;

   2: using System.ServiceModel;

   3: namespace WCFContract

   4: {

   5:     [ServiceContract]

   6:     public interface IContract

   7:     {

   8:         // This operating returns the server information, including

   9:         // the role's name and instance id

  10:         [OperationContract]

  11:         string GetRoleInfo();

  12:  

  13:         // This operation returns the information of the channel

  14:         // being used between the client & the server.

  15:         [OperationContract]

  16:         string GetCommunicationChannel();

  17:     }

  18:  }

image

2. Create a Windows Azure Project as shown below

image 

Do not add any service role at the moment, just say ok.

3. Add a new role project to the solution and select the WCF Service Web role project type in dialogue box as shown below

image

4. Add a project reference to WCF Contract project

image

5. Write the following code in Service.svc.cs file in WCFServiceWebRole project

   1: public class Service1 : WCFContract.IContract

   2: {

   3:    // Return the current web role's name and instance id

   4:   public string GetRoleInfo()

   5:   {

   6:     RoleInstance currentRoleInstance = RoleEnvironment.CurrentRoleInstance;

   7:     string RoleName = currentRoleInstance.Role.Name;

   8:     string RoleInstanceID = currentRoleInstance.Id;

   9:     return (string.Format("You are talking to role {0}, instance ID {1}\n.", 

  10:     RoleName, RoleInstanceID));

  11:   }

  12:  

  13:     // Return the channel between the client & the server

  14:     public string GetCommunicationChannel()

  15:     {

  16:         return (string.Format("We are talking via {0}.",

  17:         OperationContext.Current.Channel.LocalAddress.Uri.ToString()));

  18:        

  19:     }

  20: }

6. Change the web.config file service model section as shown below

image

7. Add a worker role to AzureWCFService project as shown in below

image

8. Define the end points for worker role in the settings as shown below

image

Important point is Web role in Azure project calls the WCF Service in the worker role via an internal point which we defined above.

9. Right click the AzureWCFService project and say publish to host on WindowsAzure environment. You will get the following window when you say publish

image

Enter your credentials to publish the service to WindowsAzure. It actually prepare the configure files for deployment.

10. Consume the WCF service in client project as shown below

   1: ChannelFactory<WCFContract.IContract> factory;

   2: WCFContract.IContract channel;

   3: // You need to modify the endpoint address to fit yours.

   4: EndpointAddress endpoint = new EndpointAddress("net.tcp://127.0.0.1:5117/External");

   5: NetTcpBinding binding = new NetTcpBinding(SecurityMode.None, false);

   6: factory = new ChannelFactory<WCFContract.IContract>(binding);

   7: channel = factory.CreateChannel(endpoint);

   8: Console.WriteLine(channel.GetRoleInfo());

   9: Console.Read();

Share this post :

About the author

Kalyan Bandarupalli

My name is kalyan, I am a software architect and builds the applications using Microsoft .NET technologies. Here I am trying to share what I feel and what I think with whoever comes along wandering to Internet home of mine.I hope that this page and its contents will speak for me and that is the reason I am not going to say anything specially about my self here.

1 Comment

TechBubbles Microsoft Technology BLOG

Follow me

Archives

Tag Cloud