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 a WCF Service which uses the Service Bus. The steps involved are the following.

Step 1 : Create a new console application

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

AppFabric Service

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.

Windows Azure AppFabricService

Step 2 : Add a reference to the System.ServiceModel assembly

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

AppFabric Service in Windows Azure

Step 3 : Add a reference to the Service Bus assemblies

Now add references 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 Files\Windows Azure AppFabric SDK\V1.5\Assemblies\NET4.0

AppFabric Service cloud computing

Step 4 : Create a WCF Service Contract and Implementation

Create a new interface named IGreetingContract and 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.

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

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

Windows Azure portal

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

AppFabric Service Bus

On clicking the View button the following dialog box appears.

AppFabric

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 behaviors 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.

 Fabric Service

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.


Similar Articles