繁体   English   中英

Azure 服务总线 - 看不到消息

[英]Azure service bus - not seeing messages

我创建了一个简单的 Azure 服务总线(队列)和一个向服务总线发送消息的客户端。 使用以下代码发送消息:

using Microsoft.Azure.ServiceBus;
using Microsoft.Extensions.Configuration;

 public async Task SendMessageAsync<T>(T message, string queueName)
        {
            try
            {
                var queueClient = new QueueClient(_config.GetConnectionString("AzureServiceBus"), queueName);
                string messageBody = JsonSerializer.Serialize(message);
                var byteMessage = new Message(Encoding.UTF8.GetBytes(messageBody));
                queueClient.SendAsync(byteMessage);
                Console.WriteLine((message as Employee).FirstName);
            }
            catch (Exception ex)
            {
                var c = ex;
            }
           
        }

发送消息使用:

using SenderApp;

Console.WriteLine("Hello, World!");
QueueService service = new QueueService();
for (int i = 0; i < 100; i++)
{
    Employee e = new Employee();
    e.FirstName = "1 " + i.ToString();
    e.LastName = "2 " + i.ToString();
    service.SendMessageAsync<Employee>(e, "employeequeue");
}

当我尝试查看活动消息时,队列中没有任何内容:

在此处输入图像描述

但是我确实看到了一些流量。 但是我发送的消息数(超过 100 条)不等于图像底部显示的传入请求数(62)。 我不确定我的消息发生了什么? 这违背了队列的目的。

请指导我为什么我没有看到任何消息。 处理这个问题的最佳方法是什么?

在此处输入图像描述

我正在使用以下 nuget 包:

<PackageReference Include="Microsoft.Azure.ServiceBus" Version="5.2.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />

除非操作失败,否则发送到 Azure 服务总线队列的消息将被传递到该队列。 在这种情况下,将抛出异常。 检查以下内容:

  1. 异常处理不会吞噬异常
  2. 等待异步发送操作以确保发送消息
  3. 用于发送的命名空间/队列是您用来接收的
  4. 没有竞争的消费者,主动从队列中接收消息。
  5. 验证 AMQP 所需的 TCP 端口未被阻止。 如果这些端口被阻止,您可以将客户端配置为使用 WebSockets。

所以我仍然不知道是什么导致了这个问题。 但我意识到Microsoft.Azure.ServiceBus package 已被弃用,后来我开始使用Azure.Messaging.ServiceBus package 连接到服务总线,一切开始工作。

我使用以下代码将消息发送到队列:

string connectionString = "Endpoint=sb://test.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=f3f+qMYTyVwE18YNl5J6ygJFi30v6J/Smph5HZvyQyE=";
            string queueName = "employeequeue";
            // since ServiceBusClient implements IAsyncDisposable we create it with "await using"
            await using var client = new ServiceBusClient(connectionString);

            // create the sender
            ServiceBusSender sender = client.CreateSender(queueName);

            // create a message that we can send. UTF-8 encoding is used when providing a string.
            ServiceBusMessage message = new ServiceBusMessage("Hello world! " + id);

            // send the message
            await sender.SendMessageAsync(message);

            return "Sent";

使用以下代码接收消息:

string queueName = "employeequeue";
            // since ServiceBusClient implements IAsyncDisposable we create it with "await using"
            await using var client = new ServiceBusClient(connectionString);
            // create a receiver that we can use to receive and settle the message
            ServiceBusReceiver receiver = client.CreateReceiver(queueName);

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

            string body = receivedMessage.Body.ToString();

            // complete the message, thereby deleting it from the service
            await receiver.CompleteMessageAsync(receivedMessage);

更多信息可用@ https://github.com/Azure/azure-sdk-for.net/blob/Azure.Messaging.ServiceBus_7.7.0/sdk/servicebus/Azure.Messaging.ServiceBus/README.md

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM