gpt4 book ai didi

azure - Azure.Messaging.ServiceBus.ServiceBusClient 的连接字符串

转载 作者:行者123 更新时间:2023-12-02 05:51:50 27 4
gpt4 key购买 nike

我正在尝试开始使用 Azure ServiceBus,但文档之神反对我。我正在关注this MS article ,这表示此代码将允许我发送消息...

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}");
}
}
<小时/>

我为 2 个常量、来自门户的连接字符串和队列名称分配了值,但是当我编译并运行它时,它无法实例化 ServiceBusClient...

System.ArgumentExceptionHResult=0x80070057Message=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

进一步挖掘,我发现短语“FullyQualifiedNamespace”被用于连接字符串,因此我用 MyServiceBusName.queue.core.windows.net 替换了我的值并运行它。结果不同但不好......

System.FormatExceptionHResult=0x80131537Message=The connection string could not be parsed; either it was malformed or contains no well-known tokens.Source=Azure.Messaging.ServiceBus

我猜测,从第一次尝试开始,就有一种方法可以将 SAS token 或共享访问 key 作为连接字符串的一部分获取,但我只是不知道如何实现。

我尝试过添加共享访问 key (SAK)

static string connectionString = 
"MyServiceBusName.queue.core.windows.net;AccountKey=accountKeyValue

没有乐趣,如果我不包含 SAK 的“名称”元素,同样如此。

同样,我也尝试添加门户给我的 SAS token ,但同样没有任何乐趣。

有人可以告诉我哪些是我应该做而我没有做的吗

更新:

更多的挖掘表明,进行以下更改应该起作用......

ServiceBusClient client = 
new ServiceBusClient(connectionString, new DefaultCredentials())
<小时/>

将我的连接字符串设置为 MyServiceBusName.queue.core.windows.net 和修改后的构造函数,它会实例化 ServiceBusClient 好的,但随后就会挂起,最终,在尝试实际发送消息时超时。

队列似乎工作正常,因为我可以在门户中添加消息并在我的计算机上使用的存储资源管理器实例中查看它们。同样,我可以在存储资源管理器中创建消息并在门户中查看它们。

最佳答案

这是为服务总线命名空间指定连接字符串的方式:

Endpoint=sb://namespacename.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=yZOy10HAJTNkyaKOqwjSricOEud7QLK7R62KyVfjCt4=

您可以通过进入 共享访问策略 > RootManageSharedAccessKey > 主要(或辅助)连接字符串从 Azure 门户获取此信息。服务总线命名空间。

更新

您似乎正在尝试使用属于 Azure 存储一部分的存储队列。如果是这种情况,那么您的连接字符串将是:

DefaultEndpointsProtocol=https;AccountName=accountname;AccountKey=xxxx==;EndpointSuffix=core.windows.net;

请注意,您无法使用 Azure.Messaging.ServiceBus 包来管理存储队列。您将需要使用Azure.Storage.Queues包。

关于azure - Azure.Messaging.ServiceBus.ServiceBusClient 的连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67818140/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com