Share via


Getting Started

To run this sample, you must first install the HPC Pack 2008 R2 Client Utilities Redistributable Package with Service Pack 1 (or higher) and the HPC Pack 2008 R2 SDK with Service Pack 1 (or higher). In addition, you need to have Microsoft Office version 2007 SP1 or higher, and administrative access to your HPC cluster’s head node and broker node.

To run the WCF service in Windows Azure nodes, you must have a valid Windows Azure account, a Windows Azure worker node template defined in your head node, and several Windows Azure worker nodes in the HPC cluster that are started and online. See the Deploying Azure Worker Nodes in Windows HPC Server 2008 R2 SP1 Step-by-Step Guide on TechNet for further information.

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

Task 1 – Inspecting the WCF Service Project

In this task, you will inspect the WCF service project to see the contract and implementation of the service.

  1. Open Microsoft Visual Studio 2010 from Start | All Programs | Microsoft Visual Studio 2010 | Microsoft Visual Studio 2010.
  2. Open the AsianOptions.sln solution file located in the AsianOptions\Source folder.
  3. In the Solution Explorer window, expand the AsianOptionsService project node, as shown in Figure 1:

    Figure 1

    The AsianOptionsService project

  4. Open the IPricingService.cs file and inspect the service contract. The service contract contains only one method: the PriceAsianOptions method.
  5. Open the PricingService.cs file (double-click the PricingService.svc file) and inspect the contents of the service. The service implementation is a loop performing Monte Carlo pricing simulations.
    1. Note the ServiceContext.Logger.TraceEvent and ServiceContext.Logger.TraceData method calls. These method calls will output debugging information to a trace file, which you will be able to see in the head node after the job finishes running.

Task 2 – Inspecting the WCF Service Configuration

In this task, you will inspect the contents of the service’s configuration file. The configuration file allows you to set how the HPC service host locates your service and hosts it. The configuration file also includes information on how to expose the service through the WCF broker node.

  1. In the AsianOptionsService project, open the AsianOptionsService.config file. Look for the <microsoft.Hpc.Session.ServiceRegistration>XML element:

    AsianOptionsService.config

    <microsoft.Hpc.Session.ServiceRegistration> <service assembly="\\MyHeadNode\Apps\AsianOptions\AsianOptionsService.dll" contract="AsianOptionsService.IPricingService" type="AsianOptionsService.PricingService" includeExceptionDetailInFaults="true" maxConcurrentCalls="0" serviceInitializationTimeout="60000" stdError="" maxMessageSize="65536"> </service> </microsoft.Hpc.Session.ServiceRegistration>
  2. The following attributes are required to configure how the service is hosted:
    1. assembly. Defines the location of the service’s assembly file, which should contain both the service contract and the service implementation.
    2. contract. Defines the fully qualified name of the service contract.
    3. type. Defines the fully qualified name of the service’s implementation class.
  3. Inside the AsianOptionsService.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.
  4. Inside the AsianOptionsService.config file, locate the <system.serviceModel> XML element and inspect the contents of that element. This XML element contains the configuration for the service’s message logging as well as the binding configuration for the endpoints exposed by the service.

Task 3 – Inspecting the Client Configuration

In this task, you will explore the client configuration used to create the new service job.

  1. In the Solution Explorer window, expand the AsianOptions project node.

    Figure 2

    The AsianOptions project

  2. In the AsianOptions project, open the app.config file and locate the <appSettings> XML element:

    C#

    <appSettings> <add key="HeadNodeName" value="HeadNodeMachine"/> <add key="SoaServiceName" value="AsianOptionsService"/> <add key="NodeGroup" value="AzureWorkerNodes"/> </appSettings>
  3. Set the HeadNodeName attribute to match your head node’s machine name.
    Note:
    There is no need to change the SoaServiceName attribute, as this attribute is currently set to the name of the tested service (AsianOptionsService).

Task 4 – Inspecting the Client Code for an Interactive Session

In this task, you will examine the client code that creates an interactive session and then calls the hosted WCF service in the HPC cluster. Interactive sessions are service jobs that host WCF services in the 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.

  1. In the AsianOptions project, right-click the ThisWorkbook.csfile, select View Code, and locate the GetSession method.
  2. The GetSession method creates a new Microsoft.Hpc.Scheduler.Session.SessionStartInfoobject and then sets its properties, as shown in the following code snippet:

    C#

    // The name of the head node in the cluster. string schedulerName = ConfigurationManager.AppSettings["HeadNodeName"]; // The name of the called service. string serviceName = ConfigurationManager.AppSettings["SoaServiceName"]; SessionStartInfo info = new SessionStartInfo(schedulerName, serviceName); info.Secure = false; info.NodeGroupList.Add( ConfigurationManager.AppSettings["NodeGroup"]);

    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.

  3. After setting the session start information, the GetSession method creates a new session object using the Microsoft.HPC.Scheduler.Session.Session class, and then creates a new PricingServiceClient object, which is a generated WCF proxy that will be used to send requests to the service. The PricingServiceClient’s constructor accepts:

    1. Binding. A NetTcpBinding object that will be used to create the client channel.
    2. EndpointAddress. The address of the WCF broker node’s endpoint. This address is stored in the session.EndpointReference property of the session object.

    C#

    Session session = Session.CreateSession(info); Client = new PricingServiceClient( new NetTcpBinding(SecurityMode.None, false), session.EndpointReference);
    Note:
    The client’s proxy uses NetTcpBinding with no security (the false parameter in the NetTcpBinding object constructor). If you change the proxy to use transport security, you will also need to change the SessionStartInfo.Secure property to true. You cannot use bindings other than the NetTcp binding.

    The Session class is responsible for creating a new job with a service task in the job scheduler. Once the session starts, the client application can send service requests to the WCF broker node that will route them to the available WCF service in the HPC cluster using the Client property.

  4. Open the Sheet1.cs file (right-click it in the Solution Explorer and select View Code), locate the Run_Clickmethod, and examine its contents. This method uses the generated proxy to call the service asynchronously and update the worksheet based on the responses returned from the WCF service, as shown in the following code snippet:

    C#

    client = Globals.Client; foreach (string col in cols) { for (int i = 2; i <= 11; i++) { client.BeginPriceAsianOptions( new PriceAsianOptionsRequest(initial, exercise, up, down, interest, periods, runs), (IAsyncResult result) => { double price = client.EndPriceAsianOptions(result) .PriceAsianOptionsResult; // Populate the cell: Cell Id is stored in result.AsyncState this.Range[(string)result.AsyncState, missing].Value2 = price; // Some more Excel work // … } ,null); } }

Task 5 – Inspecting the Client Code for a Durable Session

In this task, you will examine the client code that creates a durable session, sends requests to the WCF service hosted in the HPC cluster, and reconnects to the session at a later time to retrieve the responses returned by the hosted services. Durable sessions are different from interactive sessions in that the WCF broker queues the responses that the WCF service returns, allowing clients to disconnect from the session after sending requests to the broker node, and retrieve the responses by reconnecting to the durable session later on.

  1. In the AsianOptions project, open the Sheet2.cs file (right-click it in the Solution Explorer and select View Code). Locate the SubmitRequests method and examine its contents.
  2. This method also uses the Microsoft.Hpc.Scheduler.Session.SessionStartInfotype to define how to start the session, but this time the session is a durable session, created by the Microsoft.Hpc.Scheduler.Session.DurableSession.CreateSessionmethod, as shown in the following code snippet:

    C#

    using (DurableSession session = DurableSession.CreateSession(info)) { this.Range["D21", missing].Value2 = "Session Created."; this.Range["D20", missing].Value2 = session.Id; SubmitServiceRequests(initial, exercise, up, down, periods, runs, interestStart, interestEnd, interestStep, session); }
  3. After creating the session and sending requests to the service, the session object is disposed, but the durable session keeps running in the job scheduler, collecting responses from the running services. To connect to the running session, the DurableSession.AttachSession method is called from the RetrieveResponses method (still in the Sheet2.cs file), with an object of type Microsoft.Hpc.Scheduler.Session.SessionAttachInfo that contains the ID of the running durable session, as shown in the following code snippet:

    C#

    SessionAttachInfo attachInfo = new SessionAttachInfo(schedulerName, sessionId); Console.WriteLine("Attaching to session {0}...", sessionId); // Attach to an already running session using (DurableSession session = DurableSession.AttachSession(attachInfo)) { GetServiceResponse(results, session); // Close the session to reclaim the system storage // used to store the results. After the session is closed // you cannot attach to the same session again session.Close(); }
  4. Locate the SubmitServiceRequestsmethod and examine its contents. This method uses the BrokerClient<T> and the PriceAsianOptionsRequest classes to send service requests to the broker node.
  5. Locate the GetServiceResponse method. This method creates a BrokerClient<T> object and then iterates the service responses received from the broker node by calling the BrokerClient<T>.GetResponses<TMessage> generic method. In this method, TMessage is of type PriceAsianOptionsResponse, a message contract class representing the response type of the called service operation, as shown in the following code snippet:

    C#

    using (BrokerClient<IPricingService> client = new BrokerClient<IPricingService>(session)) { foreach (BrokerResponse<PriceAsianOptionsResponse> response in client.GetResponses<PriceAsianOptionsResponse>()) { cellContext idx = response.GetUserData<cellContext>(); double price = response.Result.PriceAsianOptionsResult; // More calculations and Excel work // … } }
  6. Build the AsianOptions solution.