gpt4 book ai didi

Azure 服务总线 SendMessageAsync 方法终止并使整个程序崩溃

转载 作者:行者123 更新时间:2023-12-03 06:50:21 28 4
gpt4 key购买 nike

我创建了一个 .NET core 6 项目。我添加了 Azure.Messaging.ServiceBus 作为依赖项。我正在使用下面的代码将消息发送到服务总线主题。

   // See https://aka.ms/new-console-template for more information
using Azure.Messaging.ServiceBus;
using System.Dynamic;
using System.Net;
using System.Text;
using System.Text.Json;

Console.WriteLine("Hello, World!");

Sender t = new Sender();
Sender.Send();

class Sender
{

public static async Task Send()
{
string connectionString = "Endpoint=sb://sb-test-one.servicebus.windows.net/;SharedAccessKeyName=manage;SharedAccessKey=8e+6SWp3skB3AeDlwH6ufGEainEs45353435JzDywz5DU=;";
string topicName = "topicone";
string subscriptionName = "subone";

// The Service Bus client types are safe to cache and use as a singleton for the lifetime
try
{

await using var client = new ServiceBusClient(connectionString, new ServiceBusClientOptions
{
TransportType = ServiceBusTransportType.AmqpWebSockets
});

// create the sender
ServiceBusSender sender = client.CreateSender(topicName);
dynamic data = new ExpandoObject();
data.name = "Abc";
data.age = 6;

// create a message that we can send. UTF-8 encoding is used when providing a string.

var messageBody = JsonSerializer.Serialize(data);

ServiceBusMessage message = new ServiceBusMessage(messageBody);
// send the message
await sender.SendMessageAsync(message);


var s = 10;

}
catch (Exception e)
{
var v = 10;
}

//// create a receiver for our subscription that we can use to receive the message
//ServiceBusReceiver receiver = client.CreateReceiver(topicName, subscriptionName);

//// the received message is a different type as it contains some service set properties
//ServiceBusReceivedMessage receivedMessage = await receiver.ReceiveMessageAsync();

//// get the message body as a string
//string body = receivedMessage.Body.ToString();
//Console.WriteLine(body);

Console.WriteLine("Press any key to end the application");
Console.ReadKey();
}
}

问题:当我在执行此行后调用await sender.SendMessageAsync(message);时,程序实际上正在终止。它不是等待。整个执行在此行之后停止。

系统没有抛出任何异常,服务总线也没有收到任何消息。

我刚刚注意到,我看到的所有其他示例在 azure 门户中默认都有一个名为 RootManageSharedAccessKey 的默认 SharedAccessPolicy 策略。对我来说,我必须制定这项政策。根据我的策略,我提供了“管理”、“发送”、“接收访问”。

最佳答案

需要更改Sender.Send();到 Sender.Send().GetAwaiter().GetResult();

关于Azure 服务总线 SendMessageAsync 方法终止并使整个程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73747169/

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