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.