Home > Back-end >  Service Bus Connection String using the Azure.ServiceBus.Messaging C# SDK for SAS token
Service Bus Connection String using the Azure.ServiceBus.Messaging C# SDK for SAS token

Time:12-13

I am trying to migrate from the Legacy Azure service bus SDK to the new one "Azure.Messaging.ServiceBus". But it seems many of the functionalities that were there in the old ones are not supported. How do I generate a service bus connection string using a given Sas Token? The older one could it do with the "ServiceBusConnectionBuilder". (https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.servicebus.servicebusconnectionstringbuilder.-ctor?view=azure-dotnet#Microsoft_Azure_ServiceBus_ServiceBusConnectionStringBuilder__ctor_System_String_) . How can I do the same with the latest SDK?

CodePudding user response:

To use a SAS token, we recommend that you construct the ServiceBusClient using the overload that accepts an AzureSasCredential rather than a connection string. This has the advantage of allowing the SAS Token to be updated without needing to destroy and recreate the client.

For example:

var credential = new AzureSasCredential("<< SHARED ACCESS KEY STRING >>");
var fullyQualifiedNamespace = "<< NAMESPACE (likely similar to {your-namespace}.servicebus.windows.net) >>";

await using var client = new ServiceBusClient(fullyQualifiedNamespace, credential);

// ...

Though we do not recommend or publicize it, the SharedAccessSignature token is supported in a connection string, resulting in something like:

"Endpoint=sb://<<namespace-name>>.servicebus.windows.net;SharedAccessSignature=<<SAS TOKEN>>"
  • Related