1

In the code sample in documentation for Microsoft ServiceBus following code is used to make sure that the topic exists.

// Create the topic if it does not exist already
string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");

var namespaceManager = 
NamespaceManager.CreateFromConnectionString(connectionString);

if (!namespaceManager.TopicExists("TestTopic"))
{
    namespaceManager.CreateTopic("TestTopic");
}

But I want to know how expensive the TopicExists call will be, if I put this code before sending the message. (assume that I don't want to have initialization code separately)

Alternative approach is to be optimistic and send the message without checking for topic existence and handling MessagingEntityNotFoundException. In case of the exception we can create the topic and retry sending the message.

The second approach seems better to me, but I couldn't find any reference supporting it. So I want to know that, is there a particular reason that Microsoft in their documentation and samples chose the first approach rather than handling the exception.

Ammar
  • 233
  • 1
  • 8
  • If its expect that its may not found: check it. If its unexpected: handle it. – Grim Feb 04 '15 at 14:36
  • @Peter I agree with the idea, but here for the first time, in a new environment, it is expected to be not found, but after that it is unexpected that topic is not there. – Ammar Feb 04 '15 at 14:51
  • But hm .... this indeed is a different situation! Do you have a installer/setup or integration-process that brings your software into a installed/integrated/configured process? Or any starting process that is like a installation/integration? If not, you must check it first. If yes, check it while integration/installation and handle it while running. – Grim Feb 04 '15 at 15:10
  • @Peter Currently there is no setup/installer mechanism, I was going with the strategy of creating/verifying things when and where you need them, instead of setting up everything up front. (or just assume every thing is there and handle exception when something is amiss) – Ammar Feb 05 '15 at 11:38

1 Answers1

0

One thing to bear in mind is that you need Manage permissions to the bus to create a topic. You may not want to grant this level of permission to all your clients as this can be a bit of a security risk, e.g. a client could create a subscription to read messages it's not supposed to see.

Calling TopicExists() before opening a client connection isn't very expensive and will give rise to more graceful code. If you wait for an exception to be tripped before creating anything then you may find you have a slew of failed messages on your hands.

I normally have a separate process for creating and updating the structure of the bus. How practical this is depends on how many topics and queues you are creating.

Ben Morris
  • 585
  • 3
  • 6