1

I have a Microsoft Azure Service Bus connection string that I want to validate if it is a valid one. I want to validate the topics and subscriptions as well if they exist. If the connection string, topics, and subscriptions are invalid then we can take appropriate actions.

The sample connection string can be like - TransportType=AmqpWebSockets;Endpoint=sb://myservicebus.servicebus.windows.net/;SharedAccessKeyName=sendkey;SharedAccessKey=MQHnx3voLhH/xVgoamX3KijzkZ0qb7U6oHTolj7LM9H=;

I tried ServiceBusClient but it is not having such support. Is there a way we can validate the connection string and topics/subscriptions?

0xced
  • 25,219
  • 10
  • 103
  • 255
Reyan Chougle
  • 4,917
  • 2
  • 30
  • 57
  • When you say "validate" is your intent to verify that the connection string is well-formed or that a specific entity exists. Also, would you be so kind as to clarify which SDK package you're using? – Jesse Squire Feb 24 '22 at 17:04
  • 1
    @JesseSquire I want to check if the given connection string is able to connect to the endpoint. Just like a ping to the endpoint. I am using "Azure.Messaging.ServiceBus" nuget package – Reyan Chougle Feb 25 '22 at 07:06

1 Answers1

2

The client will validate that the connection string and entity names are well-formed, but there is no dedicated operation for testing its ability to communicate with Service Bus.

Connections and links are established lazily when the first network operation that requires them is invoked. There are two approaches that occur to me which would trigger network resource creation without being intrusive.

Create a message batch

In order to ensure that the batch size does not exceed the limits for a given resource, the ServiceBusSender must query the queue/topic. In the case where the client is misconfigured, it will throw.

// In a real scenario, you would want to create these as
// singletons and reuse them for the lifetime of the application.

await using var client = new ServiceBusClient("<< Connection String>>");
await using var sender = client.CreateSender("<< Queue/Topic >>");

var valid = true;

try
{
    using var batch = await sender.CreateMessageBatchAsync().ConfigureAwait(false);
}
catch (Exception ex)
    when (ex is ServiceBusException 
        || ex is IOException 
        || ex is SocketException)
{
    valid = false;
}

Peek a message

Peeking a message will not cause the message to be locked or otherwise impact receiving it.

await using var client = new ServiceBusClient("<< Connection String>>");
await using var receiver = client.CreateReceiver("<< Queue/Subscription >>");

var valid = true;

try
{
    _ = await receiver.PeekMessageAsync().ConfigureAwait(false);
}
catch (Exception ex)
    when (ex is ServiceBusException 
        || ex is IOException 
        || ex is SocketException)
{
    valid = false;
}

Interactions with each queue, topic, and subscription make use of a dedicated AMQP link, so each would need to be tested individually if you'd like to ensure that the name matches a known Service Bus entity.

0xced
  • 25,219
  • 10
  • 103
  • 255
Jesse Squire
  • 6,107
  • 1
  • 27
  • 30