I am trying to use to Azure Service Bus to broadcast a message from a Web Role to all the instances of a single worker role. This is the code I use to receive messages:
// Create the topic if it does not exist already
string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
// Configure Topic Settings
TopicDescription td = new TopicDescription("CommandTopic");
td.MaxSizeInMegabytes = 5120;
td.DefaultMessageTimeToLive = new TimeSpan(0, 0, 1);
if (!namespaceManager.TopicExists("CommandTopic"))
{
namespaceManager.CreateTopic(td);
}
Random rand = new Random();
double randNum = rand.Next();
if (!namespaceManager.SubscriptionExists("CommandTopic", "CommandSubscription"+randNum))
{
namespaceManager.CreateSubscription("CommandTopic", "CommandSubscription" + randNum);
}
Client = SubscriptionClient.CreateFromConnectionString(connectionString, "CommandTopic", "CommandSubscription" + randNum, ReceiveMode.ReceiveAndDelete);
Trace.WriteLine("SUBSCRIPTION: COMMANDSUBSCRIPTION"+randNum);
In order to create a separate subscription for each worker role instance (so that all instances receive the message in the topic) I had to use a random number. Is there a way of using some Id of the instance instead of the random number. There is Instance.Id however it is too long to be used as a parameter for the subscription name. Is there a shorter version without using substring? Also, is creating a separate subscription for each instance the proper approach? Previously all instances subscribed to the same subscription and so only 1 instance was getting the message and deleting it from the subscription.