This article is a continuation of the previous two.
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.
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.
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.
Step 3: Add reference to Service Bus assemblies
Now add reference to the following assemblies.
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
Step 4: Create WCF Service Contract and Implementation
Create a new interface named IGreetingContract add the new method as shown below.
public interface IGreetingContract
string GetMessage(string name);
Create a new class named GreetingService and implement the above method as shown below.
[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.
In the appearing dialog select the Service Bus and click on the View button for the Default Key property from the right side.
On clicking the View button the following dialog box appears.
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();
Console.WriteLine(“Service Ready At: ” + serviceEndPoint.Address);
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.
Please note the url starting with sb which is the default prefix for Service Bus.
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.