EasilyNET.RabbitBus.AspNetCore 4.25.513.101

dotnet add package EasilyNET.RabbitBus.AspNetCore --version 4.25.513.101
                    
NuGet\Install-Package EasilyNET.RabbitBus.AspNetCore -Version 4.25.513.101
                    
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="EasilyNET.RabbitBus.AspNetCore" Version="4.25.513.101" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EasilyNET.RabbitBus.AspNetCore" Version="4.25.513.101" />
                    
Directory.Packages.props
<PackageReference Include="EasilyNET.RabbitBus.AspNetCore" />
                    
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 EasilyNET.RabbitBus.AspNetCore --version 4.25.513.101
                    
#r "nuget: EasilyNET.RabbitBus.AspNetCore, 4.25.513.101"
                    
#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.
#addin nuget:?package=EasilyNET.RabbitBus.AspNetCore&version=4.25.513.101
                    
Install EasilyNET.RabbitBus.AspNetCore as a Cake Addin
#tool nuget:?package=EasilyNET.RabbitBus.AspNetCore&version=4.25.513.101
                    
Install EasilyNET.RabbitBus.AspNetCore as a Cake Tool
EasilyNET.RabbitBus.AspNetCore

支持延时队列,服务端需要启用 rabbitmq-delayed-message-exchange 插件

  • 支持同一个消息被多个 Handler 消费
  • 若是就是想写多个 Handler 但是又希望某些 Handler 不执行,可以在不需要的 Handler 上标记 [IgnoreHandler] 特性
如何使用
  • 首先使用 Nuget 包管理工具添加依赖 EasilyNET.RabbitBus.AspNetCore
  • 等待下载完成和同意开源协议后,即可使用本库.
  • Step1.在 Program.cs 中配置消息总线
// 配置服务(亦可使用集群模式或者使用配置文件,或者环境变量.)
builder.Services.AddRabbitBus(c =>
{
    c.Host = "192.168.2.110";
    c.Port = 5672;
    c.UserName = "username";
    c.PassWord = "password";
    c.PoolCount = (uint)Environment.ProcessorCount;
    c.RetryCount = 5;
    ...
});
  • Step2.接下来配置事件和事件处理器
/// <summary>
/// 测试消息类型,消息继承自 IEvent 或者 Event
/// </summary>
[Exchange("hoyo.test", EModel.Routing, "test", "orderqueue2")]
public class TestEvent : Event
{
    /// <summary>
    /// 消息
    /// </summary>
    public string Message { get; set; } = default!;
}

/// <summary>
/// 消息处理Handler
/// </summary>
public class TestEventHandler(ILogger<TestEventHandler> logger) : IEventHandler<TestEvent>
{
    /// <summary>
    /// 当消息到达的时候执行的Action
    /// </summary>
    /// <param name="event"></param>
    /// <returns></returns>
    public Task HandleAsync(TestEvent @event)
    {
        logger.LogInformation("TestEvent_{event}-----{date}", @event.Message, DateTime.Now);
        return Task.CompletedTask;
    }
}

/// <summary>
/// 若是存在同一个消息多个 Handler 实现,比如这里我们写了两个 Handler,那么发送一次消息这两个 Handler 均会执行.
/// </summary>
/// 若是不希望这个 Handler 执行可以标记
[IgnoreHandler]
public class TestEventHandlerSecond(ILogger<TestEventHandlerSecond> logger) : IEventHandler<TestEvent>
{
    /// <summary>
    /// 当消息到达的时候执行的Action
    /// </summary>
    /// <param name="event"></param>
    /// <returns></returns>
    public Task HandleAsync(TestEvent @event)
    {
        logger.LogInformation("TestEvent_{event}-----{date}", @event.Message, DateTime.Now);
        return Task.CompletedTask;
    }
}
  • Step3.使用消息队列发送消息
private readonly IBus _ibus;
// 控制器构造函数伪代码
construct(IBus ibus){
   _ibus = ibus;
}
/// <summary>
/// 创建一个延时消息,同时发送一个普通消息做对比
/// </summary>
[HttpPost("TTLTest")]
public async Task TTLTest()
{
    var rand = new Random();
    var ttl = rand.Next(1000, 10000);
    var ttlobj = new DelayedMessageEvent() { Message = $"延迟{ttl}毫秒,当前时间{DateTime.Now:yyyy-MM-dd HH:mm:ss}" };
    // 延时队列需要服务端安装延时队列插件.
    await _ibus.Publish(ttlobj, (uint)ttl);
    await _ibus.Publish(ttlobj);
}
使用自定义序列化器
  • 默认序列化器是 System.Text.Json,若是需要使用其他序列化器,可以实现 IBusSerializer 接口,然后在配置中指定序列化器.
  • 若是单独开启一个项目用于该实现,推荐使用基础 Nuget 包 EasilyNET.RabbitBus.Core 然后实现 IBusSerializer 接口即可.这里以 MessagePack 为例.
/// <summary>
/// MessagePackSerializer
/// </summary>
public sealed class MsgPackSerializer : IBusSerializer
{
    private static readonly MessagePackSerializerOptions standardOptions =
        MessagePackSerializerOptions.Standard
                                    .WithResolver(CompositeResolver.Create(NativeDateTimeResolver.Instance, // 使用本地日期时间解析器
                                        ContractlessStandardResolver.Instance))                             // 使用无合约标准解析器
                                    .WithSecurity(MessagePackSecurity.UntrustedData);                       // 设置安全选项以处理不受信任的数据

    /// <summary>
    /// 使用 LZ4 算法对整个数组进行压缩.这种方式适用于需要对大量数据进行压缩的场景,压缩效率较高
    /// </summary>
    private static readonly MessagePackSerializerOptions lz4BlockArrayOptions =
        standardOptions.WithCompression(MessagePackCompression.Lz4BlockArray);

    /// <summary>
    /// 使用 LZ4 算法对每个数据块进行压缩.这种方式适用于需要对单个数据块进行压缩的场景,压缩速度较快
    /// </summary>
    private static readonly MessagePackSerializerOptions lz4BlockOptions =
        standardOptions.WithCompression(MessagePackCompression.Lz4Block);

    /// <inheritdoc />
    public byte[] Serialize(object? obj, Type type)
    {
        var data = MessagePackSerializer.Serialize(type, obj, standardOptions);
        var options = data.Length > 8192 ? lz4BlockArrayOptions : lz4BlockOptions;
        return MessagePackSerializer.Serialize(type, obj, options);
    }

    /// <inheritdoc />
    public object? Deserialize(byte[] data, Type type)
    {
        var options = data.Length > 8192 ? lz4BlockArrayOptions : lz4BlockOptions;
        return MessagePackSerializer.Deserialize(type, data, options);
    }
}
  • 然后调整服务注册代码添加如下内容
// 配置服务(亦可使用集群模式或者使用配置文件,或者环境变量.)
builder.Services.AddRabbitBus(c =>
{
    ...
    c.BusSerializer = new MsgPackSerializer();
    ...
});
Product Compatible and additional computed target framework versions.
.NET 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 is compatible.  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. 
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
4.25.513.101 100 4 days ago
4.25.506.150 113 11 days ago
4.25.429.162 121 18 days ago
4.25.429.103 144 18 days ago
4.25.411.142 111 a month ago
4.25.409.92 130 a month ago
4.25.403.133 129 a month ago
4.25.319.113 140 2 months ago
4.25.312.103 144 2 months ago
4.25.227.135 78 3 months ago
4.25.221.115 106 3 months ago
4.25.212.95 228 3 months ago
4.25.211.140 83 3 months ago
4.25.124.223 63 4 months ago
4.25.116.110 94 4 months ago
4.25.115.121 61 4 months ago
4.25.114.172 93 4 months ago
4.25.109.111 91 4 months ago
4.25.108.182 90 4 months ago
4.25.108.160 85 4 months ago
4.25.1.1 119 5 months ago
3.24.1224.141 102 5 months ago
3.24.1216.116 114 5 months ago
3.24.1206.100 75 5 months ago
3.24.1205.171 81 5 months ago
3.24.1202.150 76 5 months ago
3.24.1126.231 79 6 months ago
3.24.1126.172 74 6 months ago
3.24.1126.114 74 6 months ago
3.24.1126.104 73 6 months ago
3.24.1125.181 67 6 months ago
3.24.1125.104 74 6 months ago
3.24.1121.183 70 6 months ago
3.24.1120.183 69 6 months ago
3.24.1119.31 71 6 months ago
3.24.1115.143 60 6 months ago
3.24.1113.100 75 6 months ago
3.24.1112.125 563 6 months ago
3.24.1107.140 68 6 months ago
3.24.1107.54 68 6 months ago
3.24.1107.34 70 6 months ago
3.24.1105.111 73 6 months ago
3.24.1103.31 78 6 months ago
3.24.1103 75 6 months ago
3.24.1031.135 68 7 months ago
3.24.1031.112 69 7 months ago
3.24.1031.104 72 7 months ago
3.24.1029.142 74 7 months ago
3.24.1025.30 168 7 months ago
3.24.1022.142 63 7 months ago
3.24.1018.204 126 7 months ago
3.24.1018.175 118 7 months ago
3.24.1018.166 117 7 months ago
3.24.1018.93 122 7 months ago
3.24.1017.42 71 7 months ago
3.24.1016.161 73 7 months ago
3.24.1015.231 72 7 months ago
3.24.1015.14 74 7 months ago
3.24.1012.114 69 7 months ago
3.24.1009.115 75 7 months ago
3.24.1008.160 73 7 months ago
3.24.1008.133 67 7 months ago
3.24.1007.185 71 7 months ago
3.24.1003.33 78 7 months ago
3.24.1002.162 77 7 months ago
3.24.929.143 75 8 months ago
3.24.929.141 73 8 months ago
3.24.929.131 72 8 months ago
3.24.929.122 75 8 months ago
3.24.926.184 75 8 months ago
3.24.926.182 75 8 months ago
3.24.926.175 79 8 months ago
3.24.924.160 76 8 months ago
3.24.924.133 83 8 months ago
3.24.924.124 74 8 months ago
3.24.924.10 77 8 months ago
3.24.924.1 68 8 months ago
3.24.923.234 72 8 months ago
3.24.923.232 75 8 months ago
3.24.923.155 72 8 months ago
3.24.919.92 86 8 months ago
3.24.914.125 79 8 months ago
3.24.914.115 75 8 months ago
3.24.914.111 76 8 months ago
3.24.911.95 75 8 months ago
3.24.908.215 69 8 months ago
3.24.904.200 75 8 months ago
3.24.828.163 83 9 months ago
3.24.820.173 84 9 months ago
3.24.814.92 92 9 months ago
3.24.812.115 86 9 months ago
3.24.802.100 65 10 months ago
3.24.801.162 70 10 months ago
3.24.801.160 73 10 months ago
3.24.801.155 73 10 months ago
3.24.730.164 62 10 months ago
3.24.730.91 58 10 months ago
3.24.724.91 68 10 months ago
3.24.718.105 90 10 months ago
3.24.716.95 79 7/16/2024
3.24.712.94 79 7/12/2024
3.24.710.14 74 7/9/2024
3.24.709.105 80 7/9/2024
3.24.704.94 76 7/4/2024
3.24.701.90 75 7/1/2024
3.24.628.114 82 6/28/2024
3.24.627.145 77 6/27/2024
3.24.620.160 83 6/20/2024
3.24.613.115 78 6/13/2024
3.24.612.95 82 6/12/2024
3.24.528.90 77 5/28/2024
3.24.522.84 93 5/22/2024
3.24.512.213 75 5/12/2024
3.24.508.112 92 5/8/2024
2.2024.428.71 87 4/28/2024
2.2024.427.1128 87 4/27/2024
2.2.72 97 4/14/2024
2.2.71 77 4/12/2024
2.2.8 82 4/26/2024
2.2.6 77 4/10/2024
2.2.5 99 3/26/2024
2.2.4 96 3/25/2024
2.2.3 87 3/24/2024
2.2.2 92 3/21/2024
2.2.1 95 3/20/2024
2.2.0 100 3/13/2024
2.1.9 94 2/21/2024
2.1.8 95 2/18/2024
2.1.7 95 2/16/2024
2.1.6 110 2/14/2024
2.1.5 87 2/14/2024
2.1.4 110 2/9/2024
2.1.3 91 2/8/2024
2.1.2 119 2/5/2024
2.1.1.2 168 12/26/2023
2.1.1.1 103 12/26/2023
2.1.1 106 12/25/2023
2.1.0 122 12/17/2023
2.0.11 144 12/6/2023
2.0.1 141 11/15/2023
2.0.0 98 11/14/2023
1.9.1 113 11/1/2023
1.9.0 107 10/19/2023
1.9.0-preview2 225 10/12/2023
1.9.0-preview1 83 10/12/2023
1.8.9 133 10/11/2023
1.8.8 126 10/11/2023
1.8.7-rc2 89 9/21/2023
1.8.7-rc1 86 9/12/2023
1.8.6 129 8/31/2023
1.8.5 553 8/25/2023
1.8.4 118 8/24/2023
1.8.3 116 8/23/2023
1.8.2 158 8/22/2023
1.8.1 120 8/18/2023
1.8.0 123 8/15/2023
1.7.9 145 8/11/2023
1.7.8 119 8/11/2023
1.7.7 132 8/10/2023
1.7.6 126 8/9/2023
1.7.5 149 8/9/2023
1.7.4 167 8/3/2023
1.7.3 134 8/1/2023
1.7.2 132 7/31/2023
1.7.1 126 7/27/2023
1.7.0 132 7/25/2023
1.6.9 133 7/25/2023
1.6.8 122 7/24/2023
1.6.7 138 7/20/2023
1.6.6 133 7/19/2023
1.6.5 104 7/19/2023
1.6.4 124 7/17/2023
1.6.3 120 7/17/2023
1.6.2 140 7/12/2023
1.6.1 138 6/30/2023
1.6.0 121 6/30/2023