Munan.RabbitMqEvent 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Munan.RabbitMqEvent --version 1.0.0                
NuGet\Install-Package Munan.RabbitMqEvent -Version 1.0.0                
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="Munan.RabbitMqEvent" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Munan.RabbitMqEvent --version 1.0.0                
#r "nuget: Munan.RabbitMqEvent, 1.0.0"                
#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 Munan.RabbitMqEvent as a Cake Addin
#addin nuget:?package=Munan.RabbitMqEvent&version=1.0.0

// Install Munan.RabbitMqEvent as a Cake Tool
#tool nuget:?package=Munan.RabbitMqEvent&version=1.0.0                

Munan.RabbitMqEvent

A simple RabbitMq event handling plugin / 一个简单的 RabbitMq 事件处理插件

Just one function / 就一个功能

Realize that producers and consumers match by parameter type / 实现生产者和消费者通过参数类型进行匹配

Example

setting / 配置

{
    "RabbitMq": {
        "Host": "localhost",
        "UserName": "guest",
        "Password": "guest"
     }
}
builder.Services.AddRabbitMqEvent(opt => {
    var config = builder.Configuration.GetSection("RabbitMq");
    opt.HostName = config["Host"];
    opt.UserName = config["UserName"];
    opt.Password = config["Password"];
    return opt;
});

Or define your own / 或者你自己定义

// This is the configuration class / 这个是配置类
public class RabbitMqOptions
{
    public string HostName { get; set; } = "localhost";
    public string UserName { get; set; } = "guest";
    public string Password { get; set; } = "guest";

    public string QueueName { get; set; }

    public string ExchangeName { get; set; }

    public string RoutingKey { get; set; }

    public RabbitMqOptions(string? _queueName)
    {
        QueueName = _queueName ?? "_MyRabbitMqQueue";
        ExchangeName = $"{QueueName}-exchange";
        RoutingKey = $"{QueueName}-key";
    }
}

Parameter class / 参数类

public record TestMqObj(int Id, string Name) : IMqEventParam;

Consumer / 消费者

// It is necessary to inherit MqHandler and implement the Handle method
// 需要继承 MqHandler,实现 Handle 方法
// Support dependency injection, type is singleton
// 支持依赖注入,类型是单例
public class TestMqHandler : MqHandler<TestMqObj>
{

    private readonly IServiceProvider _services;
	 
    public TestMqHandler(IServiceProvider services)
    {
        _services = services;
    }

    public override void Handle(TestMqObj message)
    {
        Thread.Sleep(1000);
        using var scope = _services.CreateScope();
        var di2 = scope.ServiceProvider.GetRequiredService<TestDI2>();
        /*
        builder.Services.AddScoped<TestDI2>();
        public class TestDI2
        {
            public string Text { get; set; }

            public TestDI2()
            {
                Text = "TestDI2";
            }
        }
        */
        Console.WriteLine($"{message.Id} : {message.Name} {di2.Text}");
    }
}

public class TestMqHandler2 : MqHandler<TestMqObj>
{
    private readonly TestDI _TestDI;

    public TestMqHandler2(TestDI testDI)
    {
        _TestDI = testDI;
    }
    /*
    builder.Services.AddSingleton<TestDI>();
    public class TestDI
    {
        public string Text { get; set; }

        public TestDI()
        {
            Text = "TestDI";
        }
    }
    */

    public override void Handle(TestMqObj message)
    {
        Console.WriteLine($"{message.Id} : {message.Name} 2 {_TestDI.Text}");
    }
}

Producer / 生产者

private readonly MqHeIper _mqHeIper;

public TestController(MqHeIper mqHeIper)
{
    _rabbitMqHeIper = rabbitMqHeIper;
}

[HttpGet]
public async Task<ActionResult> TestEvent () {
    // Note the parameter type here TestMqObj
    // 注意这里的参数类型 TestMqObj
    _mqHeIper.Publish(new TestMqObj(1, "abc"));
    return Ok("ok");
}

That'll be fine / 这样就行了

Product Compatible and additional computed target framework versions.
.NET 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. 
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
1.0.2 198 6/13/2023
1.0.1 157 6/13/2023
1.0.0 145 6/7/2023