Windows Azure - WCF in Web Role

In this article I am going to demonstrate the following

  • Creation of a WCF service in the Web Role
  • Deployment to the cloud
  • Testing with a client

The attributes of a WCF service on the Web role are:

  • IIS Hosted
  • IIS features like pooling and caching availability
  • Less Configurations Needed

The following are the steps involved.

Step 1: Create the WCF Service

Create a new Windows Azure project in Visual Studio 2010 and name it as WCFInWebRole as shown below

WebWinAzr1.gif

In the appearing dialog add one Web Role project as shown below.

WebWinAzr2.gif

Add a new WCF Service into the project. Name it as MessengerService.

WebWinAzr3.gif

Now the Solution Explore will look like below with the newly added contracts and service files.

WebWinAzr4.gif

Modify the DoWork() method in the interface IMessengerService as given below.

string SendMessage(string name);

Modify the DoWork() method in the interface IMessengerService as given below.

public string SendMessage(string name)
{
   
return "Hello " + name;
}


Step 2: Test the application

Make the MessengerService.svc file as the start page as shown below.

WebWinAzr5.gif

Press F5 to execute the application and you can see the service opened in the browser as shown below.

WebWinAzr6.gif

Now are have tested the application and it is ready to be deployed to the cloud.

Step 3: Deploy to the cloud

Right click on the Web Role project and click package menu item as shown below.

WebWinAzr7.gif

In the appearing dialog select the default option and click the Package button.

WebWinAzr8.gif

Now sign in to the Windows Azure portal and click on the New Hosted Service button from the top ribbon.

WebWinAzr9.gif

In the appearing dialog box enter the details as shown below.

WebWinAzr10.gif

Choose the option "Deploy to production environment"

Locate the package and configuration files from your application bin folder.
(Eg: ..\WCFInWebRole\bin\Debug\app.publish)

Please note that the URL prefix should be a unique name. If the URL prefix entered is already in use you need to change it.

After that click the Ok button of the dialog box. (You have to scroll down to see the button)

If any warning box appears, click Yes button to continue. Wait for a few minutes and your deployment will be ready in a few minutes as shown below.

WebWinAzr11.gif

Select the Deployment 1 row and from the properties window you can get the url of the web role.

WebWinAzr12.gif

Click on the url and your application will get opened in the browser. Add the suffix MessengerService.svc to get the messenger service.

WebWinAzr13.gif


Step 4: Create the Client

Now we are ready to create the client application and test the deployed online cloud WCF service.

Create a new console application into the existing Azure project and name it as TestClient.

Use the add service reference option of the console application and in the appearing dialog enter the service url as shown below.

WebWinAzr14.gif

Click Ok button to continue.

Modify the main method in the Program.cs as following:

static void Main(string[] args)
{
    ServiceReference1.
MessengerServiceClient client = new ServiceReference1.MessengerServiceClient();
   
string result = client.SendMessage("Ian");

    Console.WriteLine(string.Format("Invoking WCF Service Result: {0}", result));

    Console.ReadKey(false);
}

Now set the console application as the start project and execute the application.

You can see the following results.

WebWinAzr15.gif

You can see the result Hello Ian from the WCF service.

This concludes our article on WCF service deployment as web role and the testing.

Summary

In this article we have seen how to deploy a WCF service to cloud platform. The WCF service inside the Web role will be hosted in IIS. The source code is attached and the application name in the url has to be changed according to your application name.
 


Similar Articles