HMENetCore.RabbitMQ 6.0.47

dotnet add package HMENetCore.RabbitMQ --version 6.0.47
                    
NuGet\Install-Package HMENetCore.RabbitMQ -Version 6.0.47
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HMENetCore.RabbitMQ" Version="6.0.47" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HMENetCore.RabbitMQ" Version="6.0.47" />
                    
Directory.Packages.props
<PackageReference Include="HMENetCore.RabbitMQ" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add HMENetCore.RabbitMQ --version 6.0.47
                    
#r "nuget: HMENetCore.RabbitMQ, 6.0.47"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package HMENetCore.RabbitMQ@6.0.47
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=HMENetCore.RabbitMQ&version=6.0.47
                    
Install as a Cake Addin
#tool nuget:?package=HMENetCore.RabbitMQ&version=6.0.47
                    
Install as a Cake Tool

HMENetCore.RabbitMQ

简介

HMENetCore.RabbitMQ 基于 RabbitMQ .NET 客户端的封装库,提供了更简单易用的 API 来处理 RabbitMQ 的连接、生产者和消费者操作。

功能特性

  • 自动连接管理,支持断线重连
  • 多主机支持,自动故障转移
  • 线程安全的连接和通道管理
  • 支持多种消息模式:普通模式/Work模式、发布/订阅模式、路由模式、Topic模式
  • 消息确认和拒绝机制
  • 消息限流控制

.NET支持

  • 跨平台支持: Supports .NET Standard 2.1, .NET 6, .NET 8, and .NET 9.

安装

dotnet add package HMENetCore.RabbitMQ --version 6.0.47

配置连接

添加配置到 appsettings.json

{
  "RabbitMQConfig": {
    "Hosts": [ "rabbitmq1", "rabbitmq2", "rabbitmq3" ],
    "Port": 5672,
    "UserName": "guest",
    "Password": "guest",
    "VirtualHost": "/"
  }
}

服务注册

var rabbitMQConfig = Configuration.GetSection("RabbitMQConfig").Get<RabbitMQConfig>();
builder.Services.AddRabbitMQSetup(rabbitMQConfig);

生产者示例

public class MessageService
{
    private readonly RabbitMQProducer _producer;
    
    public MessageService(RabbitMQProducer producer)
    {
        _producer = producer;
    }
    
    public async Task SendMessageAsync(string queueName, string message)
    {
        await _producer.PublishAsync(queueName, message, options =>
        {
            options.Durable = true; // 持久化队列
        });
    }
    
    public async Task SendToExchangeAsync(string exchange, string routingKey, string message)
    {
        await _producer.PublishAsync(exchange, routingKey, message, options =>
        {
            options.Type = RabbitMQExchangeType.Direct;
            options.Durable = true;
        });
    }
}

消费者示例

public class MessageProcessor : IHostedService, IDisposable
{
    private readonly RabbitMQConsumer _consumer;
    private readonly ILogger<MessageProcessor> _logger;
    private ListenResult _listenResult;
    
    public MessageProcessor(RabbitMQConsumer consumer, ILogger<MessageProcessor> logger)
    {
        _consumer = consumer;
        _logger = logger;
    }
    
    public async Task StartAsync(CancellationToken cancellationToken)
    {
        _consumer.Received += OnMessageReceived;
        
        // 开始监听队列
        _listenResult = await _consumer.ListenAsync("my_queue", options =>
        {
            options.AutoAck = false; // 手动确认
            options.Durable = true;
            options.FetchCount = 10; // 每次获取10条消息
        });
    }
    
    private void OnMessageReceived(RecieveResult result)
    {
        try
        {
            _logger.LogInformation($"Received message: {result.Body}");
            // 处理消息...
            
            // 确认消息处理完成
            result.Commit();
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to process message");
            // 不调用Commit(),消息会被重新入队
        }
    }
    
    public Task StopAsync(CancellationToken cancellationToken)
    {
        _listenResult?.Stop();
        _consumer.Received -= OnMessageReceived;
        return Task.CompletedTask;
    }
    
    public void Dispose()
    {
        _listenResult?.Stop();
    }
}

其他用法

//批量获取消息
var message = await _consumer.ReceiveGetAsync("my_queue", new ConsumeQueueOptions
{
    FetchCount = 5
});

if (!string.IsNullOrEmpty(message))
{
    Console.WriteLine($"Received: {message}");
}


//获取队列消息数量
var count = await _consumer.GetMQMessageCountAsync("my_queue");
Console.WriteLine($"Messages in queue: {count}");


//使用交换机和路由键
// 生产者
await _producer.PublishAsync("my_exchange", "my.routing.key", "Hello RabbitMQ", options =>
{
    options.Type = RabbitMQExchangeType.Topic;
    options.Durable = true;
});

// 消费者
await _consumer.ListenAsync("my_exchange", "my_queue", options =>
{
    options.RoutingKeys = new[] { "my.*" }; // 订阅所有以my.开头的路由键
    options.AutoAck = false;
});

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
6.0.47 136 8/12/2025
6.0.46 209 8/7/2025
6.0.45 128 7/16/2025
6.0.42 149 6/18/2025
6.0.40 275 6/10/2025
6.0.39 233 5/15/2025
6.0.32 178 4/9/2025
6.0.31 166 3/17/2025
6.0.30 135 2/19/2025
6.0.10 122 2/5/2025
6.0.8 137 11/15/2024
6.0.2 130 10/9/2024
6.0.1 189 8/16/2024