I'm trying to get started with Azure ServiceBus, but the Gods of Documentation are against me. I'm following this MS article, which says this code will allow me to send a message...
public class Program
{
static string connectionString = "<NAMESPACE CONNECTION STRING>";
static string queueName = "<QUEUE NAME>";
static async Task Main(string[] args)
{
await SendMessageAsync();
}
static async Task SendMessageAsync()
{
// create a Service Bus client
ServiceBusClient client = new ServiceBusClient(connectionString);
// create a sender for the queue
ServiceBusSender sender = client.CreateSender(queueName);
// create a message that we can send
ServiceBusMessage message = new ServiceBusMessage("Hello world!");
// send the message
await sender.SendMessageAsync(message);
Console.WriteLine($"Sent a single message to the queue: {queueName}");
}
}
I assigned values to the 2 constants, the connection string from the portal and the name of the queue, but when I compile and run it it fails on the line to instantiate the ServiceBusClient
...
System.ArgumentException HResult=0x80070057 Message=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') Source=Azure.Messaging.ServiceBus
More digging and I discover the phrase "FullyQualifiedNamespace" being used for the connection string, so I replaced the value I had with MyServiceBusName.queue.core.windows.net
and run that. The results are different but not good...
System.FormatException HResult=0x80131537 Message=The connection string could not be parsed; either it was malformed or contains no well-known tokens. Source=Azure.Messaging.ServiceBus
I'm guessing that from the first attempt that there is a way of getting the SAS Token or Shared Access Key in as part of the connection string, I just can't work out how.
I've tried just tacking on the Shared Access Key (SAK)
static string connectionString =
"MyServiceBusName.queue.core.windows.net;AccountKey=accountKeyValue
No joy, same if I don't include the "name" element of the SAK.
likewise I've tried adding the SAS Token that the portal gave me, again with no joy.
Can someone please tell me what I'm not doing that I should be, please
UPDATE:
More digging suggests that making the following change should also work...
ServiceBusClient client =
new ServiceBusClient(connectionString, new DefaultCredentials())
With my connection string just set to MyServiceBusName.queue.core.windows.net
and the modified constructor, it gets instantiates ServiceBusClient
OK, but then just hangs and, ultimately, times out when trying to actually send a message.
The queue appears to be working OK as I can add messages in the portal and see them in the instance of Storage Explorer that I'm using on my machine. Likewise I can create message in Storage Explorer and see them in the Portal.
This is how the connection string should be specified for a Service Bus Namespace:
Endpoint=sb://namespacename.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=yZOy10HAJTNkyaKOqwjSricOEud7QLK7R62KyVfjCt4=
You can get this information from Azure Portal by going into Shared Access Policy
> RootManageSharedAccessKey
> Primary (or Secondary) Connection String
for your Service Bus Namespace.
UPDATE
It seems you're trying to use Storage Queues which are part of Azure Storage. If that is the case, then your connection string would be:
DefaultEndpointsProtocol=https;AccountName=accountname;AccountKey=xxxx==;EndpointSuffix=core.windows.net;
Please note that you cannot use Azure.Messaging.ServiceBus
package to manage your Storage Queues. You will need to use Azure.Storage.Queues
package.