NetPro.EasyNetQ 6.0.16

dotnet add package NetPro.EasyNetQ --version 6.0.16                
NuGet\Install-Package NetPro.EasyNetQ -Version 6.0.16                
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="NetPro.EasyNetQ" Version="6.0.16" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NetPro.EasyNetQ --version 6.0.16                
#r "nuget: NetPro.EasyNetQ, 6.0.16"                
#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.
// Install NetPro.EasyNetQ as a Cake Addin
#addin nuget:?package=NetPro.EasyNetQ&version=6.0.16

// Install NetPro.EasyNetQ as a Cake Tool
#tool nuget:?package=NetPro.EasyNetQ&version=6.0.16                

EasyNetQ使用

NuGet

对EasyNetQ的封装,简化使用,支持多库

使用

appsetting.json
"EasyNetQOption": {
    "Idle": 60,//默认60秒对象持续
    "ConnectionString": [
      {
        "Key": "rabbit2", //连接串key别名,唯一
        "Value": "host=192.168.18.129:5672;virtualHost=/;username=admin;password=123456;timeout=60" //别名key对应的连接串
      },
      {
        "Key": "rabbit1", //连接串key别名,唯一
        "Value": "host=192.168.18.129:5672;virtualHost=/;username=admin;password=123456;timeout=60" //别名key对应的连接串
      }
    ]
  },

启用服务

没有基于NetPro.Web.Api 的使用场景,必须手动进行初始化,如下:

IConfiguration Configuration;

public void ConfigureServices(IServiceCollection services,IConfiguration configuration)
{    
        services.AddEasyNetQ().Build(configuration);
}

基于NetPro.Web.Api的使用,只需要添加引用后配置以上appsetting.josn配置EasyNetQOption节点即可

使用说明

发布者
    /// <summary>
    /// rabbitmq发布者
    /// </summary>
 public class EasyNetQService : IRabbitmqService
    {
        private readonly IdleBus<IBus> _idbus;//适合用于发布者使用,内部有对象管理机制如用于订阅有连接断开可能的错误。
        private static bool _stop;
        public EasyNetQService(IdleBus<IBus> idbus)
        {
            _idbus = idbus;
        }


         public async Task PublishAsync(string dbKey = "rabbit1")
        {
            _idbus.Get(dbKey).PubSub.PublishAsync(new RabbitMessageModel { Text = $"[{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}]this is a message" })
                  .ContinueWith(p =>
                  {
                      if (p.Exception != null)
                          p.Exception.Handle(x =>
                          {
                              Console.WriteLine(x.Message);
                              return true;
                          });
                  });
       }
    }
  /// <summary>
  /// 定义rabbitmq 的队列,交换机等
  /// </summary>
 [Queue("my_queue_name", ExchangeName = "my_exchange_name_")]
    public class RabbitMessageModel
    {
        /// <summary>
        /// 
        /// </summary>
        public string Text { get; set; }
    }
消费者
    /// <summary>
    /// rabbitmq消费者
    /// </summary>
    internal class RabbitMqStartTask : IStartupTask//消费者只需要初始化一次,放在后台服务或系统初始化操作中
    {
        private readonly IEasyNetQMulti _easyNetQMulti;//适合用于订阅者使用,无需using包裹保持对象不销毁以达到持续订阅。
       
        public RabbitMqStartTask()
        {
            _easyNetQMulti = EngineContext.Current.Resolve<IEasyNetQMulti>();
        }
        public int Order => 0;

        public void Execute()
        {
            //订阅/Subscribe
            //订阅需要保持长连接,请使用EasyNetQMulti获取连接对象并且不用使用using和调用dispose()
            var bus = _easyNetQMulti["rabbit1"];
            //同交换机,同队列下subscriptionId为订阅唯一标识,相同标识会依次收到订阅消息,类似于广播
            bus.PubSub.Subscribe<RabbitMessageModel>("subscriptionId_", tm =>
            {
                Console.WriteLine("Recieve Message: {0}", tm.Text);
            });
        }
    }
性能:

alternate text is missing from this package README image

高级用法:
  • 自定义方式获取连接串
public class XXXEasyNetQ : INetProStartup
{
    public double Order { get; set; } = int.MaxValue;

    public void ConfigureServices(IServiceCollection services, IConfiguration? configuration = null, ITypeFinder? typeFinder = null)
    {
        services.AddEasyNetQ(GetConnectionString);
    }

    public void Configure(IApplicationBuilder application, IWebHostEnvironment env)
    {
    }

    public IList<ConnectionString> GetConnectionString(IServiceProvider serviceProvider)
    {
        var connector = new List<ConnectionString>
        {
            new ConnectionString { Key = "2", Value = "host=192.168.100.187:5672;virtualHost=my_vhost;username=admin;password=admin;timeout=60" }
        };
        return connector;
    }
}
更新中...

reference 使用rabbitmq消息队列——EasyNetQ插件介绍

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 was computed.  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. 
.NET Core netcoreapp3.1 is compatible. 
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.16 225 7/24/2023
6.0.15 461 7/19/2022
6.0.14 451 7/10/2022
6.0.13 461 6/15/2022
6.0.12 402 6/15/2022
6.0.11 423 6/15/2022
6.0.10 425 6/11/2022
6.0.9 428 6/8/2022
6.0.8 485 5/26/2022
6.0.8-beta.3 128 5/24/2022
6.0.8-beta.2 121 5/24/2022
6.0.7 435 5/18/2022
6.0.6 440 4/28/2022
6.0.5 458 3/30/2022
6.0.5-beta.20 118 4/27/2022
6.0.5-beta.19 131 4/25/2022
6.0.5-beta.18 133 4/22/2022
6.0.5-beta.17 141 4/16/2022
6.0.5-beta.16 140 4/8/2022
6.0.5-beta.15 126 4/8/2022
6.0.5-beta.14 138 4/7/2022
6.0.5-beta.13 130 4/7/2022
6.0.5-beta.12 123 4/6/2022
6.0.5-beta.11 130 4/6/2022
6.0.5-beta.10 128 3/31/2022
6.0.5-beta.9 149 3/26/2022
6.0.5-beta.8 144 3/22/2022
6.0.5-beta.7 124 3/21/2022
6.0.5-beta.6 142 3/14/2022
6.0.5-beta.5 133 3/2/2022
6.0.5-beta.4 134 2/22/2022
6.0.5-beta.3 131 2/18/2022
6.0.5-beta.2 129 2/18/2022
6.0.5-beta.1 131 2/16/2022
6.0.4 430 2/10/2022
6.0.3 453 2/9/2022
6.0.3-beta.9 123 2/10/2022
6.0.3-beta.7 156 1/27/2022
6.0.3-beta.6 163 1/19/2022
6.0.3-beta.5 149 1/18/2022