public void Setup(string subscription, string priority)
{
var namespaceManager = NamespaceManager.CreateFromConnectionString(this.serviceBusConnectionString);
// Setup the topic.
if (!namespaceManager.TopicExists(this.topicName))
{
try
{
namespaceManager.CreateTopic(this.topicName);
}
catch (MessagingEntityAlreadyExistsException)
{
Trace.TraceInformation("Messaging entity already created: " + this.topicName);
}
catch (MessagingException ex)
{
var webException = ex.InnerException as WebException;
if (webException != null)
{
var response = webException.Response as HttpWebResponse;
// It's likely the conflicting operation being performed by the service bus is another queue create operation
// If we don't have a web response with status code 'Conflict' it's another exception
if (response == null || response.StatusCode != HttpStatusCode.Conflict)
{
throw;
}
Trace.TraceWarning("MessagingException HttpStatusCode.Conflict - Queue likely already exists or is being created or deleted for path: {0}", this.topicName);
}
}
}
this.topicClient = TopicClient.CreateFromConnectionString(this.serviceBusConnectionString, this.topicName);
this.topicClient.RetryPolicy = RetryPolicy.Default;
// Setup the subscription.
if (!string.IsNullOrEmpty(subscription))
{
if (!namespaceManager.SubscriptionExists(this.topicName, subscription))
{
// Setup the filter for the subscription based on the priority.
var filter = new SqlFilter("Priority = '" + priority + "'");
var ruleDescription = new RuleDescription
{
Name = "PriorityFilter",
Filter = filter
};
try
{
namespaceManager.CreateSubscription(this.topicName, subscription, ruleDescription);
}
catch (MessagingEntityAlreadyExistsException)
{
Trace.TraceInformation("Messaging entity already created: " + subscription);
}
catch (MessagingException ex)
{
var webException = ex.InnerException as WebException;
if (webException != null)
{
var response = webException.Response as HttpWebResponse;
// It's likely the conflicting operation being performed by the service bus is another queue create operation
// If we don't have a web response with status code 'Conflict' it's another exception
if (response == null || response.StatusCode != HttpStatusCode.Conflict)
{
throw;
}
Trace.TraceWarning("MessagingException HttpStatusCode.Conflict - subscription likely already exists or is being created or deleted for path: {0}", subscription);
}
}
}
this.subscriptionClient = SubscriptionClient.CreateFromConnectionString(this.serviceBusConnectionString, this.topicName, subscription);
this.subscriptionClient.RetryPolicy = RetryPolicy.Default;
}
}