skip to Main Content

I am trying to automate the infrastructure of my application. As part of that, I am creating Service Bus related resources (Namespace, Topics, Subscriptions) only if they do not exist. My subscriptions will only have 1 rule. So every time the script runs, it will delete all existing rules and create this rule from scratch.

Here’s the pseudo code I am writing:

$topic = Get-AzServiceBusTopic -Name $TopicName -NamespaceName $NamespaceName -ResourceGroupName $ResourceGroupName -ErrorVariable notPresent -ErrorAction SilentlyContinue
if ($null -eq $topic -or $notPresent)
{
    $topic = New-AzServiceBusTopic -Name TopicName -NamespaceName NamespaceName -ResourceGroupName ResourceGroupName -ErrorAction Stop
}
$subscription = Get-AzServiceBusSubscription -Name $SubscriptionName -TopicName $TopicName -NamespaceName $NamespaceName -ResourceGroupName $ResourceGroupName -ErrorVariable notPresent -ErrorAction SilentlyContinue
if ($null -eq $subscription -or $notPresent)
{
    $subscription = New-AzServiceBusSubscription -Name SubscriptionName -TopicName TopicName -NamespaceName NamespaceName -ResourceGroupName ResourceGroupName -ErrorAction Stop
}
# forcefully delete existing rules
Get-AzServiceBusRule -SubscriptionName SubscriptionName -TopicName TopicName -NamespaceName NamespaceName -ResourceGroupName ResourceGroupName | Remove-AzServiceBusRule
# create rule
$rule = New-AzServiceBusRule -Name "`$Default"  -SubscriptionName SubscriptionName -TopicName TopicName -NamespaceName NamespaceName -ResourceGroupName ResourceGroupName -FilterType SqlFilter -SqlExpression SqlFilterExpression -ErrorAction Stop

Randomly I am seeing New-AzServiceBusRule Cmdlet call fails with the following error:

The messaging entity
‘namespacename:Topic:topicname|subscriptionname|$Default’
already exists. To know more visit
| https://aka.ms/sbResourceMgrExceptions. TrackingId:823eeae9-8776-46e7-90de-5ec305e14bb5_B26,
SystemTracker:NoSystemTracker,
| Timestamp:2023-05-13T10:33:37

According to the documentation of New-AzServiceBusRule, the cmdlet either creates a new rule and updates an existing rule. If that is the case, then why am I getting the resource exists exception.

I even tried waiting for a second before deleting all existing rules and creating the new rule but that did not help either.

Interesting thing is that it happens randomly. For some of the Subscriptions and Rules, the code works just fine and then randomly for one odd Subscription, it would fail. At times, it will not fail at all!

How can I prevent this from happening?

2

Answers


  1. The messaging entity ‘namespacename:Topic:topicname|subscriptionname|$Default’ already exists.

    This error shouldn’t come, as per Document and my analysis below is clearly says that this error will not come :
    With $Deafult:

    enter image description here

    enter image description here

    enter image description here

    One can clearly see I hadn’t received any error for different rules too that are already existing with me.

    If whenever I see these errors in other commands i integrate below commands into my script:

    $emo=Get-AzServiceBusRule -SubscriptionName "rithwik" -TopicName "rithwik" -NamespaceName "rithwik1" -ResourceGroupName "rgname"
    foreach ($e in $emo.Name)
    {
        Remove-AzServiceBusRule -SubscriptionName "rithwik" -TopicName "rithwik" -NamespaceName "rithwik1" -ResourceGroupName "rg name" -Name $e
    }
    $rule = New-AzServiceBusRule -Name "vammo"  -SubscriptionName "rithwik" -TopicName "rithwik" -NamespaceName "rithwik1" -ResourceGroupName "rgname" -FilterType SqlFilter -SqlExpression '1=1' -ErrorAction Stop
    

    Even with $default not getting any error :

    enter image description here

    If the issue still persists , I would suggest you to raise a support request.

    Login or Signup to reply.
  2. I’m having a problem that seems to have the same root cause. In my case I use Ansible to manage the Azure Service Bus objects. Ansible Azure Collection doesn’t have support for subscription rules so I do it through the Ansible API support in the Collection.

    Sometimes when I create a new subscription rule, I get this error. If I go to the portal, the rule was indeed created, but Ansible will stall for about 3-4 minutes until it says the operation went OK. If I go to the Activity Log I see the failed request, and some minutes later the successful one.

    @Gaurav Mantri did you ever get an answer from Microsoft?

    This is vey concerning not only because it delays actions a lot, but also because I think I have detected at least one case where the rule was created, but throw this error, and the $Default rule wasn’t removed.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search