Stu*_*ing 0 connection-string azure azureservicebus azure-servicebus-queues
我正在尝试开始使用 Azure ServiceBus,但文档之神反对我。我正在关注这篇 MS 文章,它说这段代码将允许我发送消息......
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}");
}
}
Run Code Online (Sandbox Code Playgroud)
我将值分配给 2 个常量、来自门户的连接字符串和队列的名称,但是当我编译并运行它时,它无法在线实例化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
Run Code Online (Sandbox Code Playgroud)
进一步挖掘,我发现连接字符串使用了短语“FullyQualifiedNamespace”,所以我替换了我拥有的值MyServiceBusName.queue.core.windows.net并运行它。结果是不同的,但不是很好......
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
Run Code Online (Sandbox Code Playgroud)
我猜从第一次尝试有一种方法可以将 SAS 令牌或共享访问密钥作为连接字符串的一部分,我就是不知道怎么做。
我试过只添加共享访问密钥 (SAK)
static string connectionString = "MyServiceBusName.queue.core.windows.net;AccountKey=accountKeyValue
Run Code Online (Sandbox Code Playgroud)
没有乐趣,如果我不包括 SAK 的“名称”元素,也是一样。
同样,我尝试添加门户网站给我的 SAS 令牌,但又没有任何乐趣。
有人可以告诉我我应该做的我没有做的事情吗
更新:
更多的挖掘表明进行以下更改也应该有效......
ServiceBusClient client = new ServiceBusClient(connectionString, new DefaultCredentials())
Run Code Online (Sandbox Code Playgroud)
将我的连接字符串设置为MyServiceBusName.queue.core.windows.net和修改后的构造函数,它实例化ServiceBusClient正常,但随后挂起,最终在尝试实际发送消息时超时。
队列似乎工作正常,因为我可以在门户中添加消息并在我在我的机器上使用的存储资源管理器实例中查看它们。同样,我可以在存储资源管理器中创建消息并在门户中查看它们。
这是为服务总线命名空间指定连接字符串的方式:
Endpoint=sb://namespacename.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=yZOy10HAJTNkyaKOqwjSricOEud7QLK7R62KyVfjCt4=
Run Code Online (Sandbox Code Playgroud)
你可以通过进入Shared Access Policy> RootManageSharedAccessKey>Primary (or Secondary) Connection String来获取服务总线命名空间的信息。
更新
您似乎正在尝试使用作为 Azure 存储一部分的存储队列。如果是这种情况,那么您的连接字符串将是:
DefaultEndpointsProtocol=https;AccountName=accountname;AccountKey=xxxx==;EndpointSuffix=core.windows.net;
Run Code Online (Sandbox Code Playgroud)
请注意,您不能使用Azure.Messaging.ServiceBus包来管理您的存储队列。您将需要使用Azure.Storage.Queues包。
| 归档时间: |
|
| 查看次数: |
594 次 |
| 最近记录: |