'How to read connection string from key vault for Service Bus?

Here is how I instantiate the client in my Configure method:

services.AddSingleton<ServiceBusClient>(x => new ServiceBusClient(configuration.GetSection("ServiceBus:ConnectionString").Value, serviceBusClientOptions));

And this how my appsettings looks like:

{
  "ServiceBus:ConnectionString": "@Microsoft.KeyVault(VaultName=MyVaultName;SecretName=MySecretName)"
}

However, I am getting the following exception:

The connection string used for an Service Bus client must specify the Service Bus namespace host and either a Shared Access Key (both the name and value) OR a Shared Access Signature to be valid. (Parameter 'connectionString'

What am I missing here?



Solution 1:[1]

Have you created a managed identity for you application and added access policies such that your app can GET this secret value from key vault?

Check out the official documentaion for this here : https://docs.microsoft.com/en-us/azure/app-service/app-service-key-vault-references

enter image description here

Also on a side note, have you tried directly adding the secret value as the appsetting value instead of referencing it from KV and see if that worked? (if yes then definitely its a permissions issue and NOT a problem with your C# app code.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rimaz Mohommed