Hands-On Lab

Running SOA Services with the Windows Azure HPC Scheduler

Lab version: 2.0.0

Last updated: 12/16/2011


Contents

Overview 3

Exercise 1: Creating, deploying, and submiting a SOA-based HPC application in Windows Azure 4

Task 1 - Create the Square Service and Configure it to Run in an HPC Environment 4

Task 2 - Create a Client That Uses Interactive Sessions with WCF Service Reference 8

Task 3 - Create a Client That Uses Interactive Sessions with the BrokerClient<T> Class 13

Task 4 - Deploy the WCF Service to Windows Azure Nodes 17

Task 5 - Run the Client Application in the Windows Azure Head Node 25

Verification 27

Summary 29


Overview

The HPC Square service demonstrates how to build, deploy, and execute an HPC SOA application that can run in Windows Azure. This sample includes a WCF service project and a client application that consumes it.

The client application demonstrates how to use the interactive session with both a service reference generated proxy and the BrokerClient class.

To run the client and service completely in Windows Azure, you will use the Windows Azure HPC Scheduler SDK. The SDK enables developers to create Windows Azure deployments that include parallel applications that can scale when more computation power is needed. It supports not only SOA applications, but also MPI and LINQ to HPC applications.

To manage HPC jobs, create new jobs, check currently executing jobs, and view the results of finished jobs, the Windows Azure HPC Scheduler SDK also includes a web-based portal that you can access from your local machine through a web-browser.

Note: Although this solution is for Visual Studio 2010, the AzureSquareService project uses .NET Framework 3.5; this is because the Windows Azure HPC Scheduler cannot currently host WCF services compiled for .NET Framework 4.

In this lab, you will deploy applications to Windows Azure and use the Windows Azure Management Portal. To learn more about deploying applications to Windows Azure and using the portal, refer to the Windows Azure Training Course on MSDN and run the first lab in the course, Introduction to Windows Azure.

Objectives

In this hands-on lab, you will learn how to:

· Create a WCF service and configure it to run in an HPC cluster.

· Create a WCF client application that calls the HPC SOA application using a service reference generated proxy and the BrokerClient<T> generic class.

· Deploy the Windows Azure HPC Scheduler.

· Deploy the WCF service and client application to a Windows Azure head node and compute nodes.

· Run the WCF client application from a head node.

· Use the Windows Azure HPC Scheduler Web Portal to check the status of jobs and tasks.

Prerequisites

The following is required to complete this hands-on Lab:

· Visual Studio 2010 Professional or higher with (Visual C# and Visual C++)

· Windows Azure Tools for Microsoft Visual Studio 2010 1.6

· Windows Azure HPC Scheduler SDK

· HPC Pack 2008 R2 Client Utilities Redistributable Package with Service Pack 3

Exercises

This hands-on lab includes the following exercise:

1. Creating, deploying, and submitting a SOA-based HPC application in Windows Azure.

Estimated time to complete this lab: 60 minutes.

Exercise 1: Creating, deploying, and submiting a SOA-based HPC application in Windows Azure

In this exercise, you will create a WCF service that will be hosted with the Windows Azure HPC Scheduler, a client application that submits SOA jobs to the scheduler, and then deploy the scheduler and the applications to Windows Azure. After the scheduler and the applications are deployed, you will run the client application, and use the Windows Azure HPC Scheduler web portal to verify its execution.

Task 1 - Create the Square Service and Configure it to Run in an HPC Environment

One of the benefits of running WCF services with the Windows Azure HPC Scheduler is that, in most cases, no changes need to be made to the service code. However, because of the environment transition, you will be required to generate a special configuration file that includes information about the service to be hosted.

1. Open Microsoft Visual Studio 2010 elevated as Administrator from Start | All Programs | Microsoft Visual Studio 2010 by right-clicking Microsoft Visual Studio 2010 and choosing Run as administrator.

2. Open the begin.sln solution file located in the Source\Ex01-SoaService\begin folder.

3. In the Solution Explorer window, expand the SquareService solution folder, and then expand the AzureSquareService project node.

Figure 1

The AzureSquareService project

4. Right-click the AzureSquareService project and select Add | New Item.

5. In the Add New Item dialog, select WCF Service from the Visual C# Items list, name the file SquareService.cs, and then click Add.

Figure 2

Add a new WCF service item

6. Open the ISquareService.cs file, and remove the DoWork method declaration and the decorating OperationContract attribute. The interface should resemble the following code:

C#

[ServiceContract]

public interface ISquareService

{

}

7. Add the Square method declaration to the ISquareService interface:

(Code Snippet – SOA Services - ISquareService - Square Method Declaration– CS)

C#

[ServiceContract]

public interface ISquareService

{

[OperationContract]

int Square(int num);

}

8. Open the SquareService.cs file, and remove the DoWork method implementation. The service’s code should resemble the following code:

C#

public class SquareService : ISquareService

{

}

9. The default concurrency mode of WCF is single threading, which results in the synchronization of all requests sent from a single client. To enable the service to handle multiple client requests at once, the concurrency mode of the service must be changed to multithreading by decorating the service class with the ServiceBehavior attribute and setting the ConcurrencyMode to Multiple, as shown in the following code:

(Code Snippet – SOA Services - SquareService - Add ServiceBehavior – CS)

C#

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]

public class SquareService : ISquareService

{

}

10. Add the Square method implementation to the SquareService class:

(Code Snippet – SOA Services - SquareService - Square Method Declaration– CS)

C#

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]

public class SquareService : ISquareService

{

public int Square(int num)

{

Console.WriteLine("calculating square of :" + num);

return num * num;

}

}

11. Note the Console.WriteLine method call. This call will output a message, which you will be able to see in the task results window in the Windows Azure HPC Scheduler Web Portal.

12. Right-click the AzureSquareService project and select Add | Existing Item

13. In the Add Existing Item dialog, browse to Source\Assets, change the file type to All Files (*.*), select the SquareService.config file, and click Add.

14. In the AzureSquareService project, right-click the SquareService.config file and select Properties.

15. In the Properties window, change Build Action to Content, and Copy to Output Directory to Copy always.

Figure 3

SquareService.config file properties

16. Open the SquareService.config file, and examine the contents of the file. The service configuration file allows you to set how the HPC service host locates your service and hosts it. It also includes information on how to expose the service through the WCF broker node.

17. In the SquareService.config file, look for the <microsoft.Hpc.Session.ServiceRegistration> XML element:

XML

<microsoft.Hpc.Session.ServiceRegistration>

<service assembly="AzureSquareService.dll"

contract="AzureSquareService.ISquareService"

type="AzureSquareService.SquareService"

includeExceptionDetailInFaults="true"

maxConcurrentCalls="0"

maxMessageSize="65536"

serviceInitializationTimeout="60000"

enableMessageLevelPreemption="true" >

</service>

</microsoft.Hpc.Session.ServiceRegistration>

18. The following attributes are required to configure how the service is hosted:

a. assembly. Defines the location of the service’s assembly file, which should contain both the service contract and the service implementation.

b. contract. Defines the fully qualified name of the service contract.

c. type. Defines the fully qualified name of the service’s implementation class.

Note: If you have named your WCF service something other than SquareService, change the contract and type attributes accordingly.

19. Inside the SquareService.config file, locate the <brokerServiceAddresses> XML element under the <microsoft.Hpc.Broker> | <services> element. Each of the <add> XML elements defines a base address that the WCF broker node uses to listen for client service requests.

20. Inside the SquareService.config file, locate the <system.serviceModel> XML element and inspect the contents of that element. This XML element contains the configuration for the broker service’s diagnostic tracing as well as the binding configuration for the endpoints exposed by the broker service.

21. Save the changes made to the ISquareService.cs and SquareService.cs files.

Task 2 - Create a Client That Uses Interactive Sessions with WCF Service Reference

In this task, you will add the client code that creates an interactive session. Interactive sessions are service jobs that host WCF services in the Windows Azure HPC cluster and allow interaction with the services through the WCF broker node, which routes requests and responses from the client to the cluster and back. This task uses a WCF service reference to call the hosted WCF service.

1. In the Solution Explorer window, expand the SquareService solution folder and then expand the Client project node.

Figure 4

The Client project

2. Right-click the Client project and select Add Reference.

3. In the Add Reference dialog, move to the .NET tab and add a reference to the Microsoft.HPC.Scheduler and Microsoft.HPC.Scheduler.Session assemblies. These assemblies enable the client application to submit SOA jobs to the head node and send requests to the Windows Azure HPC cluster.

Note: In case the assemblies are not added in the .NET tab, switch to Browse tab and browse to %windows%\Assembly\GAC_MSIL folder (usually, you will find Windows folder in C:\Windows). Locate the folders named Microsoft.HPC.Scheduler and Microsoft.HPC.Scheduler.Session and add the reference to the assemblies.

4. Verify the added referenced assemblies.

Figure 5

Verify the added referenced assemblies

5. Open the Program.cs file, and examine the three static variables:

C#

private static int numOfRequests = 20;

private static string headNode = Environment.GetEnvironmentVariable("CCP_SCHEDULER");

private static string serviceName = "SquareService";

Note: These three variables contain the information required to submit the SOA job:

The numOfRequests static variable controls how many square calculation requests will be submitted to the server. You can increase the number of requests if you wish, but be aware that the more requests you set, the more time it will take the cluster to calculate them.

The headNode static variable holds the name of the HPC head node to which the client submits the SOA job. The variable is set according to the CCP_SCHEDULER environment variable that exists in all Windows Azure HPC nodes, including the head node, compute nodes, and frontend node.

The serviceName static variable holds the name of the hosted SOA service that will start when the job begins running. The name of the service must match the name of the service configuration file created in the previous task.

6. Add the following namespace directive at the beginning of the Program.cs file:

(Code Snippet – SOA Services -Program - Adding Namespace directive– CS)

C#

using Microsoft.Hpc.Scheduler.Session;

7. Add the following method code to the Program class:

(Code Snippet – SOA Services -Program - InteractiveUsingServiceReference Method– CS)

C#

private static void InteractiveUsingServiceReference()

{

SessionStartInfo info = new SessionStartInfo(headNode, serviceName);

using (Session session = Session.CreateSession(info))

{

}

}

8. The method creates a new session object using the Microsoft.HPC.Scheduler.Session.Session class. The Session class is responsible for creating a new job with a service task in the Windows Azure HPC Scheduler. Once the session starts, the client application can send service requests to the WCF broker.

Note: You can also set other properties of the SessionStartInfo object to configure how the job executes. For instance, you can set the SessionResourceUnitType property to SessionUnitType.Cores and then set the MinimumUnits and MaximumUnits to define how many cores will be available for each hosted service.

9. Add the following WCF code inside the using statement to send requests to the WCF service using a service reference generated proxy:

(Code Snippet – SOA Services - Program - InteractiveUsingServiceReference WCF code– CS)

C#

private static void InteractiveUsingServiceReference()

{

SessionStartInfo info = new SessionStartInfo(headNode, serviceName);

using (Session session = Session.CreateSession(info))

{

AutoResetEvent done = new AutoResetEvent(false);

int count = 0;

Console.WriteLine("Session ID: " + session.Id);

SquareServiceClient proxy = new SquareServiceClient(

new NetTcpBinding(SecurityMode.Transport),

session.NetTcpEndpointReference);

proxy.SquareCompleted += (sender, e) =>

{

try

{

int reply = e.Result.SquareResult;

Console.WriteLine("Received response for request {0}: {1}", e.UserState, reply);

}

catch (SessionException ex)

{

Console.WriteLine("SessionException while getting responses in callback: {0}", ex.Message);

}

catch (Exception ex)

{

Console.WriteLine("Exception while getting responses in callback: {0}", ex.Message);

}

if (Interlocked.Increment(ref count) == numOfRequests)

{

done.Set();

}

};

// start to send requests

Console.WriteLine("Sending {0} requests using Service Reference proxy...", numOfRequests);

for (int i = 0; i < numOfRequests; i++)

{

proxy.SquareAsync(new SquareRequest(1000 + i), i);

}

// Main thread block here waiting for the retrieval process

// to complete. As the thread that receives the "_numOfRequests"

// responses does a Set() on the event, "done.WaitOne()" will pop

done.WaitOne();

Console.WriteLine("Done retrieving {0} responses\n", numOfRequests);

// Close connections and delete messages stored in the system

session.Close();

}

}

Note: The above method code uses the service reference class created for the square service to call the service asynchronously and print out the responses returned by the service. The session closes once the last response returns to the client.

You cannot use bindings other than the NetTcp binding to communicate with the WCF service broker.

10. Add the following method call to the Main method:

(Code Snippet – SOA Services - Program - Call InteractiveUsingServiceReference – CS)

C#

public static void Main(string[] args)

{

InteractiveUsingServiceReference();

Console.WriteLine("Press enter to exit.");

Console.Read();

}

11. Save the Program.cs file.

Task 3 - Create a Client That Uses Interactive Sessions with the BrokerClient<T> Class

In this task, you will create a method similar to that of Task 2, only this time instead of using the generated WCF service reference you will use the BrokerClient<T> generic class.

1. In the Client project, open the Program.cs file and add the following method to the Program class:

(Code Snippet – SOA Services - Program - InteractiveUsingBrokerClient Method – CS)

C#

private static void InteractiveUsingBrokerClient()

{

SessionStartInfo info = new SessionStartInfo(headNode, serviceName);

using (Session session = Session.CreateSession(info))

{

int count = 0;

AutoResetEvent done = new AutoResetEvent(false);

Console.WriteLine("Session ID: " + session.Id);

Console.WriteLine("Done retrieving {0} responses\n", numOfRequests);

// Close connections and delete messages stored in the system

session.Close();

}

}

2. Like the method in the previous task, this method also starts a new session, but this time the new method sends service requests to the WCF broker using the BrokerClient<T> generic class. To create the BrokerClient instance, add the following code inside the using statement, between the two Console.WriteLine methods:

(Code Snippet – SOA Services - Program - BrokerClient Instance – CS)

C#

private static void InteractiveUsingBrokerClient()

{

SessionStartInfo info = new SessionStartInfo(headNode, serviceName);

using (Session session = Session.CreateSession(info))

{

int count = 0;

AutoResetEvent done = new AutoResetEvent(false);

Console.WriteLine("Session ID: " + session.Id);

using (BrokerClient<ISquareService> client =

new BrokerClient<ISquareService>(session))

{

}

Console.WriteLine("Done retrieving {0} responses\n", numOfRequests);