Hosting WCF Services in Windows Azure

So you want to host a Windows Communication Foundation (WCF) service in Windows Azure? Here’s how you do it. This post will describe the steps you need to get your WCF service up and running in Windows Azure. This walkthrough assumes you are already familiar with the basics of WCF services. Hosting your services in Windows Azure has the benefit of you not having to provide the hardware for hosting your service and you can instead rely on Microsoft providing that capability for you.

Be sure you have the latest version of the Azure CTP installed and are running Windows Vista or Windows 7. Spin up Visual Studio 2008 in Administrator mode. You’ll want to be sure be sure the latest version of the CTP is installed. In this case I will be using the July 2009 CTP.

Begin by creating a Cloud Service solution. For the purpose of this demo, we’ll name it ‘AzureHostedWCF’.



When the dialog prompts you to create a new cloud service project, add a new ASP.NET Web Role project:

The web role is required as part of the cloud service.

Next, add a new Item to the web role project, namely a WCF Service item, called ‘TestWCFService’. For the purpose of this demonstration, we’ll augment the service interface as follows:

using System;

using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WebRole1
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string TellUserCurrentTime(string name);

}
}

The implementation looks like this:

using System;

using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WebRole1
{
public class Service1 : IService1
{
public string TellUserCurrentTime(string name)
{
DateTime now = DateTime.Now;
return string.Format("Hi {0}. Currently it is {1} {2}.",
name, now.ToShortDateString(), now.ToShortTimeString());
}
}
}

This service is very barebones for the purpose of this demonstration. Compile to make sure there are no errors. Right click on the cloud project (‘AzureHostedWCF’) and ‘Set as Startup Project’. Start the project in debug mode. In the browser window that opens, add ‘Service1.svc’ to the URL (the full URL in my case is http://127.0.0.1:81/Service1.svc ). This will show you that the service has been successfully created and hosted in the Azure development fabric. The development fabric simulates the Windows Azure fabric on your development machine for testing before you code is deployed.


Now that we have confirmed that the service is working in the development fabric, we are ready to publish the service to Azure. Right-click on the cloud project (‘AzureHostedWCF’) and select ‘Publish’. You will notice two things happen. First, a browser window is opened for the Windows Azure portal. Second, an explorer window is opened showing the package file and the configuration file for the cloud service. What we need to do is upload that package to Azure so it can be deployed.

Create a new hosted service in Windows Azure, we’ll call it AzureHostedWCF. Provide a description if you choose, then click the ‘Next’ button.



On the following screen a public service name needs to be provided, which is globally unique. Again, we’ll use AzureHostedWCF for the public name of the service. This can be anything you like.

Also, a region needs to be selected. For the purpose of this demonstration, we will select the ‘North Central US’. A discussion of Azure regions is beyond the scope of this demonstration. Once you have entered the information, click the ‘Create’ button.



Next, we are presented with a screen to deploy the service. Click the ‘Deploy’ button in the middle of the screen. Earlier in the Azure CTP, packages first had to be deployed to staging before they could be pushed to production. That process can now be bypassed and packages can be directly deployed to Production. Clicking on the left-facing arrow below highlighted in red will show the staging environment deployment.



On the following screen you are asked to select the application package and the configuration settings files. These are the same files that opened in the explorer window above. Locate the files and select them. You also need to add a deployment label for this service. We will use 1.0 for the purposes of this demo. Click ‘Deploy’ when you are ready.



It will take a couple of minutes for the package to deploy in Azure. When completed, you will be presented with the following screen:



Click the ‘Run’ button to start the service. Again, the package will take a few minutes to be deployed. The progress indicator under the web role name will go from a blue dot indication of ‘Stopped’, to a yellow dot indication of ‘Initializing’ and finally to a green checkmark of ‘Ready.

Once the service is ready, click on the web site URL. When the URL opens in the browser, add the service name to the end of the URL. In this case, the URL will be: http://azurehostedwcf.cloudapp.net/Service1.svc. You’ll notice the service page is now displayed.

Now we want to test that the service is properly hosted and can be consumed from Azure. Create a console test project. Add a service reference to the URL. Notice that it can’t be done. Click on the WSDL link in the above screenshot. Again, it doesn’t appear to work. There is a known bug with Azure where a proxy cannot be created from the Azure hosted service. The workaround is to create the service reference using the service hosted in the development fabric, then change the URL reference to point to the service hosted in Azure. So, go back to the ‘AzureHostedWCF’ solution, start debugging and open the service in a browser window. Now go back to test project and add a reference to the local service providing whatever namespace you choose. The code to call the service should look something like this:

using System;

using ConsumeAzureHostedWCFR.AzureHostedWCFService;

namespace ConsumeAzureHostedWCF
{
class Program
{
static void Main(string[] args)
{
Service1Client client = null;

try
{
client = new Service1Client();
string time = client.TellUserCurrentTime("Greg");

Console.WriteLine(string.Format("The service responded with: {0}", time));
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine(string.Format("{0}\n{1}", e.Message, e.StackTrace));
}
finally
{
if (client != null)
client.Close();
}
}
}
}

Finally, go into the web.config of the test project and update the address of the service endpoint to http://azurehostedwcf.cloudapp.net/Service1.svc. The node in the web.config file will look like this:

<client>

<endpoint address="http://azurehostedwcf.cloudapp.net/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="AzureHostedWCFService.IService1"
name="BasicHttpBinding_IService1" />
</client>

The output of the service call looks something like this:

This is a very simply example of an Azure hosted WCF service. More complicated services are deployed to Azure in much the same way. Go forth and deploy your WCF services to Azure!



0 comments: