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

标签 azure azureservicebus azure-servicebus-topics

我创建了一个 .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/

相关文章:

azure - 与 Docker 集成测试 Azure 服务总线

azureservicebus - Azure 服务总线超时

azure - 在 Microsoft Azure 服务总线中使用 TopicClient/QueueClient.sendAsync(IList<Message>) 发送的消息列表的大小限制 - 2020

azure - 获取 ARM 中逻辑应用的回调 url

node.js - nodejs + azure 函数应用程序 - 处理数据库连接的最佳实践?

WCF 并发连接和 Azure 服务总线中继

c# - 在 Azure 服务总线中发布到一个主题并订阅另一个主题的集成测试不可靠是否存在竞争条件?

azure - GetCurrentApplicationCallbackUri 随时间变化

azure - Azure vnet 是否会影响性能