September 30, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

How to Create a WCF Service Running on Windows Azure

  • December 13, 2011
  • By Vipul Patel
  • Send Email »
  • More Articles »

Overview

Windows Azure, Microsoft’s cloud computing platform, is not ti just web-based applications. Windows Azure offers developers a robust ability to host applications written in a variety of modes, web applications, WCF, data centric, etc.

In this article, we will walk you through the process of creating a WCF service running on Windows Azure.

Hands-On

To start with, create a new Windows Azure Project (under the installed templates -> Visual C# -> cloud). Call the project WindowsAzureWCFDemo. Click OK.

Image 1

On the “New Windows Azure Project dialog”, select WCF Service Web Role and add it to the solution.

Click OK to proceed.

Image 2

The skeleton WCF service will now be created. By default, Visual Studio will create a WCF service which implements the following interface defined in IService.cs.

Listing 1 - IService.cs

[ServiceContract]

public interface IService1

{

[OperationContract]

string GetData(int value);

[OperationContract]

CompositeType GetDataUsingDataContract(CompositeType composite);

// TODO: Add your service operations here

}

For our demo, let us change the contents of IService.cs to as under:.

[ServiceContract]

public interface IService1

{

[OperationContract]

int GetData(int left, int right);

// TODO: Add your service operations here

In the code above, we define a GetData method with takes in 2 integers and returns an integer.

Once you have changed the interface definition, let us go ahead and change the code where the interface is implemented. In our case, by default, the interface is implemented in Service1.svc.cs.

Open the Service1.svc.cs file and change the code as shown below.

Image 3

namespace WCFServiceWebRole1

{

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.

public class Service1 : IService1

{

public int GetData(int left, int right)

{

return left + right;

}

}

}

Here, we are implementing GetData to add the two numbers which are provided to the method and return the sum of the numbers as the return value.

Now, if your Visual Studio instance is not running with elevated privileges, you will need to restart it as Administrator. You need to do that because Windows Azure emulator needs to be run as elevated.

Once you have Visual Studio running elevated, you can execute F5 to start debugging the application.

Once the application is built and deployed in the cloud emulator, you should see a screen as under.

This screen shows that we have succeeded to create a WCF service. We can now proceed to deploy the service to the cloud using the instructions discussed in this article.

Now, let's proceed to create a client which consumes this WCF service. For the demo purposes, we will use the service created above, but the steps to target a Azure servce running in the cloud are essentially the same.

Start another instance of Visual Studio and create a new Visual C# Windows console application (Let’s call it WindowsAzureWCFClient). Now add a Service Reference to the project as shown below.

Image 4

You need to right click on Reference and select “Add service reference…”.

Next, enter the URI of the service in the Address field and click GO.

Image 5

Here you can also enter the cloud URI of your service if you have already packaged and deployed your WCF service to the Azure cloud.

After the services are listed, you can click OK to create a reference to the WCF service in the WindowsAzureWCFClient project.

Now, we can proceed to call our service in the WCF client application.

Here is the simple implementation of calling the WCF service. Feel free to come up with your own if you want to make it a fun exercise.

namespace WindowsAzureWCFClient

{

class Program

{

static void Main(string[] args)

{

Service1Client wcfClient = new Service1Client();

int sum = wcfClient.GetData(10, 20);

if (sum != 30)

{

Console.WriteLine("We did not get the right sum value from the service");

}

else

{

Console.WriteLine("Our WCF service returned the right sum value");

}

}

}

}

You can see that in the highlighted section above, we create an instance of the service proxy on the client and call the GetData method on the service to make it perform the addition operation on the numbers provided.

Now, when we debug the client application, we can single step and find that if the service is available and accessible, our client application will be able to successfully add the numbers and we will have the “Our WCF service returned the right sum value” printed on the console.

We have now created a WCF client application which calls a WCF service hosted in Azure cloud/emulator. If you are having problems following along, you can download the client sample code from here and the Demo itself here.

Summary

In this article, we learned how to create a WCF Service running on Windows Azure. I hope you have found this information useful.

About the author

Vipul Patel is a Software Engineer currently working at Microsoft Corporation. He is currently working in the Microsoft Lync team and has worked in the .NET team earlier in the Base Class libraries and the Debugging and Profiling team. He can be reached at vipul_d_patel@hotmail.com

 



Originally published on http://www.developer.com.


Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel