This sample demonstrates using the NetEventRelayBinding binding on the Windows Azure Service Bus. This binding allows multiple applications to listen to events sent to an endpoint; events sent to that endpoint are received by all applications.

The application accepts one of three mutually exclusive, optional command line parameters that select the connectivity mode for the Service Bus environment.

Prerequisites

If you haven't already done so, read the release notes document that explains how to sign up for a Windows Azure account and how to configure your environment. It also contains important information about the default security settings for your environment that you need to be aware of.

Service Contract & Implementation

This sample implements a chatroom via the project's IMulticastContract and MulticastService implementations. Hello and Bye are used within the chatroom application to notify participants when a user joins and leaves the chat. Chat is called by the application when a user provides a string to contribute to the conversation.

Note that the methods must be marked as IsOneWay=True.

C# 
[ServiceContract(Name = "IMulticastContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
public interface IMulticastContract
{
     [OperationContract(IsOneWay=true)]
     void Hello(string nickName);
 
     [OperationContract(IsOneWay = true)]
     void Chat(string nickName, string text);
 
     [OperationContract(IsOneWay = true)]
     void Bye(string nickName);
}

The service implementation is shown below.

C# 
ServiceBehavior(Name = "MulticastService", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
class MulticastService : IMulticastContract
{
    void IMulticastContract.Hello(string nickName)
    {
        Console.WriteLine("[" + nickName + "] joins");
    }
 
    void IMulticastContract.Chat(string nickName, string text)
    {
        Console.WriteLine("[" + nickName + "] says: " + text);
    }
 
    void IMulticastContract.Bye(string nickName)
    {
        Console.WriteLine("[" + nickName + "] leaves");
    }   
}

Configuration

The service and client endpoints use the NetEventRelayBinding binding.

Xml 
<netEventRelayBinding>
     <binding name="default" />
</netEventRelayBinding> 

The endpoints for the service and client are defined in the application configuration file. The client address is a placeholder that is replaced in the application. The following endpoints are defined:

Xml 
<service name="Microsoft.ServiceBus.Samples.MulticastService">
    <endpoint name="RelayEndpoint"
              contract="Microsoft.ServiceBus.Samples.IMulticastContract"
              binding="netEventRelayBinding"
              bindingConfiguration="default"
              address="" />
</service>
 
<client>
    <endpoint name="RelayEndpoint"
              contract="Microsoft.ServiceBus.Samples.IMulticastContract"
              binding="netTcpRelayBinding"
              bindingConfiguration="default"
              address="http://AddressToBeReplacedInCode/" />
</client>

Application

The application begins by obtaining the chat session name, the service namespace, the issuer credentials and a chat nickname (a string used to identify the chatter). The sample constructs the service URI using this information, then opens the service and the client channel to the Service Bus rendezvous endpoint for the chat session. The Hello method notifies all participating applications of the arrival of a new user. The Chat method sends all strings as messages to all participating applications until an empty string is provided as input. At that point the client leaves the chatroom and the Bye method notifies all participants of the client's departure.

C# 
Console.Write("What do you want to call your chat session? ");
string session = Console.ReadLine();
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();
Console.Write("Your Chat Nickname: ");
string chatNickname = Console.ReadLine();
TransportClientEndpointBehavior relayCredentials = new TransportClientEndpointBehavior();
relayCredentials.TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerSecret);    

Uri serviceAddress = ServiceBusEnvironment.CreateServiceUri("sb", "ChatRooms",
String.Format(CultureInfo.InvariantCulture, "{0}/MulticastService/", session));
ServiceHost host = new ServiceHost(typeof(MulticastService), serviceAddress);
host.Description.Endpoints[0].Behaviors.Add(relayCredentials);
host.Open();

ChannelFactory<IMulticastChannel> channelFactory = new ChannelFactory<IMulticastChannel>("RelayEndpoint", new EndpointAddress(serviceAddress));
channelFactory.Endpoint.Behaviors.Add(relayCredentials);
IMulticastChannel channel = channelFactory.CreateChannel();
channel.Open();

Console.WriteLine("\nPress [Enter] to exit\n");

channel.Hello(chatNickname);

string input = Console.ReadLine();
while (input != String.Empty)
{
   channel.Chat(chatNickname, input);
   input = Console.ReadLine();
}

channel.Bye(chatNickname);

channel.Close();
channelFactory.Close();
host.Close();

Building and Running the Sample

After building the solution, perform the following steps to run the application:

  1. From a command prompt, run the application bin\Debug\MulticastSample.exe.

  2. From another command prompt, run another instance of the application bin\Debug\MulticastSample.exe.

When finished, press ENTER to exit the application.

Expected Output – Application 1

What do you want to call your chat session? <chat-session>
Your Service Namespace: <service-namespace>
Your Issuer Name: owner
Your Issuer Secret: <issuer-secret>
Your Chat Nickname: <chat-nickname>

Press [Enter] to exit

[jill] joins
hello
[jill] says: hello
[jack] says: hi, how are you?
[jack] says: who do you think will win the superbowl this year?

Expected Output – Application 2

What do you want to call your chat session? <chat-session>
Your Service Namespace: <service-namespace>
Your Issuer Name: owner
Your Issuer Secret: <issuer-secret>
Your Chat Nickname: <chat-nickname>

Press [Enter] to exit

[jack] joins
[jill] joins
[jill] says: hello
hi, how are you?
[jack] says: hi, how are you?
who do you think will win the superbowl this year?
[jack] says: who do you think will win the superbowl this year?



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