This sample demonstrates how to use the Windows Azure Service Bus to route messages to multiple receivers.  It shows multiple instances of a simple service communicating with a client via the NetTcpRelayBinding binding. When each instance of the service application is started, it prompts for your credentials and opens a unique endpoint on the Service Bus. Once opened, this endpoint has a well-known URI on the Service Bus and is reachable from anywhere, even if your computer resides behind a firewall or Network Address Translation (NAT).

Clients accessing an endpoint must have permission to communicate with that endpoint. Therefore, the client application also prompts for your credentials, authenticates with the Access Control (AC) service, and acquires an access token that proves to the Service Bus infrastructure that the client is authorized to access the endpoint. Once the client is connected, you can type messages into the client application which will be echoed back by any one of the running instances of the service.

Prerequisites

If you haven't already done so, please read the release notes document that explains how to sign up for a Windows Azure account and how to configure your environment.

LoadBalance Service

The service is similar to the Echo Sample and implements a simple contract with a single operation named Echo. Every running instance of the service accepts a string and echoes the string back.

C# 
 [ServiceBehavior(Name = "EchoService", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
class EchoService : IEchoContract
{
   public string Echo(string text)
   {
      Console.WriteLine("Echoing: {0}", text);
      return text; 
   }
}

LoadBalance Client

When started, the client asks for the service namespace, creates a channel to the logical address of the router, and sends requests. Instead of the simple ChannelFactory used in the Echo sample, the LoadBalance sample uses a BalancingChannelFactory, which facilitates load balancing across the listener instances to which the router routes the client messages. Once the interaction is complete, the client closes the channel and exits.

C# 
Console.Write("Your Service Namespace: ");
string serviceNamespace = Console.ReadLine();
Console.Write("Your Issuer Name: ");
string issuerName = Console.ReadLine();
Console.Write("Your Issuer Secret: ");
string issuerSecret = Console.ReadLine();

// create the service URI based on the service namespace
Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("sb", issuerName, "EchoService");

// create the credentials object for the endpoint
TransportClientEndpointBehavior sharedSecretServiceBusCredential = new TransportClientEndpointBehavior();
sharedSecretServiceBusCredential.TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerSecret);

// create the channel factory loading the configuration
BalancingChannelFactory<IEchoChannel> channelFactory = 
    new BalancingChannelFactory<IEchoChannel>(
    new NetTcpRelayBinding(EndToEndSecurityMode.None, RelayClientAuthenticationType.RelayAccessToken), 
    new EndpointAddress(serviceUri));

// apply the Service Bus credentials
channelFactory.Endpoint.Behaviors.Add(sharedSecretServiceBusCredential);

Console.WriteLine("Enter text to echo (or [Enter] to exit):");

string input = Console.ReadLine();

while (input != String.Empty)
{
IEchoChannel channel = channelFactory.CreateChannel();
channel.Open();

try
{
// create and open the client channel
Console.WriteLine("Server echoed: {0}", channel.Echo(input));
channel.Close();
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
channel.Abort();
}

input = Console.ReadLine();
}

channelFactory.Close();

Running the Sample

To run the sample, build the solution in Visual Studio or from the command line. Run several instances of the service application first, then run one instance of the client.

When the service and the client are running, you can start typing messages into the client application. These messages are received by the service application and echoed to each instance in a round-robin fashion.

Expected Output - Service

Your Service Namespace: <service-namespace>
Your Issuer Name: <issuer-name>
Your Issuer Secret: <issuer-secret>
Service address: sb://<service-namespace>.servicebus.windows.net/EchoService/
Listen address: sb://<service-namespace>.servicebus.windows.net/EchoService/
Press [Enter] to exit
Echoing: Hello, World!

Expected Output - Client

Your Service Namespace: <service-namespace>
Your Issuer Name: <issuer-name>
Your Issuer Secret: <issuer-secret>
Enter text to echo (or [Enter] to exit): Hello, World!
Server echoed: Hello, World!


Did you find this information useful? Please send your suggestions and comments about the documentation.