Windows Azure – AppFabric Service – Server Creation

This article is a continuation of the previous two.

  1. AppFabric Introduction
  2. Service Bus Application

Service Bus Theory

Service Bus is one of the AppFabric services in the cloud platform.

Service Bus provides the facility for disconnected applications to communicate with each other. In this article we are focusing on creating a WCF service which uses the Service Bus facilities. Later we will use a client to communicate to the same service through the Service Bus.

In this article we are focusing on the activity: Creating WCF Service using Service Bus. The steps involved are following.

Step 1: Create new console application

The console application is fine for our purpose. Create a new console application.

clip_image002[4]

Now change the project property Target Framework to .Net Framework 4 as shown below. This is needed because the Microsoft.ServiceBus assembly has dependency on the above framework assembly.

clip_image004[4]

Step 2: Add reference to System.ServiceModel assembly

For the WCF contracts, we need to add reference to the System.ServiceModel assembly as shown below.

clip_image006[4]

Step 3: Add reference to Service Bus assemblies

Now add reference to the following assemblies.

  • Microsoft.ServiceBus
  • RelayConfigurationInstaller

The above files can be located in your AppFabric SDK installation folder. In my computer it was located in the following folder.

C:Program FilesWindows Azure AppFabric SDKV1.5AssembliesNET4.0

clip_image008[4]

Step 4: Create WCF Service Contract and Implementation

Create a new interface named IGreetingContract add the new method as shown below.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

namespace AppFabricWCFService

{

[ServiceContract(Name=”IGreetingContract”, Namespace=”http://tempuri.org/”)]

public interface IGreetingContract

{

[OperationContract]

string GetMessage(string name);

}

}

Create a new class named GreetingService and implement the above method as shown below.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

namespace AppFabricWCFService

{

[ServiceBehavior(Name = “IGreetingContract”, Namespace = “http://tempuri.org”)]

public class GreetingService : IGreetingContract

{

public string GetMessage(string name)

{

return “Hello, ” + name;

}

}

}

Step 5: Get the Service Bus Properties

Now we can expose the service using the Service Bus properties. For this we need the following:

  • Service Bus Namespace
  • Issuer Name
  • Issuer Secret

The Service Bus Namespace was already created in the previous article. You can refere it here.

The Issuer Name and Issuer Secret can be located following the steps.

Sign in to the Windows Azure portal and select the following item.

clip_image010[4]

In the appearing dialog select the Service Bus and click on the View button for the Default Key property from the right side.

clip_image012[4]

On clicking the View button the following dialog box appears.

clip_image014[4]

The above window provides the Issuer Name and Issuer Secret. Copy the values for use in the next step.

Step 6: Host the WCF service using Service Bus

Now we are ready to host the WCF service. In the Program.cs modify the main method as shown below. Replace the highlighted areas with your corresponding service bus properties.

static void Main(string[] args)

{

string servicePath = “GreetingService”;

string serviceNamespace = “YourNameSpaceHere”;

string issuerName = “YourIssuerNameHere”;

string issuerSecret = “YourIssuerSecretHere”;

Uri uri = ServiceBusEnvironment.CreateServiceUri(“sb”, serviceNamespace, servicePath);

TransportClientEndpointBehavior sharedSecretServiceBusCredential = new TransportClientEndpointBehavior();

sharedSecretServiceBusCredential.CredentialType = TransportClientCredentialType.SharedSecret;

sharedSecretServiceBusCredential.Credentials.SharedSecret.IssuerName = issuerName;

sharedSecretServiceBusCredential.Credentials.SharedSecret.IssuerSecret = issuerSecret;

ServiceHost host = new ServiceHost(typeof(GreetingService), uri);

ContractDescription contractDescription = ContractDescription.GetContract(typeof(IGreetingContract), typeof(GreetingService));

ServiceEndpoint serviceEndPoint = new ServiceEndpoint(contractDescription);

serviceEndPoint.Address = new EndpointAddress(uri);

serviceEndPoint.Binding = new NetTcpRelayBinding();

serviceEndPoint.Behaviors.Add(sharedSecretServiceBusCredential);

host.Description.Endpoints.Add(serviceEndPoint);

host.Open();

Console.WriteLine(“Service Ready At: ” + serviceEndPoint.Address);

Console.ReadKey(false);

host.Close();

}

You can see that we are using the Relay version of NetTcp binding.

Note: The above code does not depend on the configuration file for end points and behaviours as it is for learning purposes. In the real life scenario the configuration file should be used for the same.

Step 7: Execute the application

Now press F5 and execute the application. You should be able to see the console window showing the service ready message.

clip_image016[4]

Please note the url starting with sb which is the default prefix for Service Bus.

Summary

In this article we have created a WCF service and hosted using the AppFabric Service Bus feature. In the upcoming article we can see how to create a client to use the service. The attached source code contains the service code which you can use it after modifying using your service bus properties.

Leave a Reply

Your email address will not be published. Required fields are marked *