Hania.NetCore.RabbitMQ 2.2.2

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package Hania.NetCore.RabbitMQ --version 2.2.2
NuGet\Install-Package Hania.NetCore.RabbitMQ -Version 2.2.2
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="Hania.NetCore.RabbitMQ" Version="2.2.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Hania.NetCore.RabbitMQ --version 2.2.2
#r "nuget: Hania.NetCore.RabbitMQ, 2.2.2"
#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 Hania.NetCore.RabbitMQ as a Cake Addin
#addin nuget:?package=Hania.NetCore.RabbitMQ&version=2.2.2

// Install Hania.NetCore.RabbitMQ as a Cake Tool
#tool nuget:?package=Hania.NetCore.RabbitMQ&version=2.2.2

Hania.NetCore.RabbitMQ

Build status NuGet Author Linkdin

What is Hania.NetCore.RabbitMQ?

Reactive RabbitMQ Library To improve and make better use of RabbitMQ Message-Broker in .Net Core 3.1 or above

Where can I get it?

First, install NuGet. Then, install Hania.NetCore.RabbitMQ from the package manager console:

PM> Install-Package Hania.NetCore.RabbitMQ

How do I get started?

RabbitMQ consists of two parts : Producers and Consumers

Producer

RabbitMQ Producer writes AMQP messages to a single RabbitMQ queue.

When you configure the destination, you specify the information needed to connect to RabbitMQ client. You also define a queue and the bindings to use. You can use multiple bindings to write messages to one or more exchanges. You can also configure SSL/TLS properties, including default transport protocols and cipher suites. You can optionally configure AMQP message properties.

Consumer

RabbitMQ is a messaging broker. It accepts messages from publishers, routes them and, if there were queues to route to, stores them for consumption or immediately delivers to consumers, if any.

Consumers consume from queues. In order to consume messages there has to be a queue. When a new consumer is added, assuming there are already messages ready in the queue, deliveries will start immediately.

Initialize RMQ Client

1 - First of all you need to add haniarabbitmq section to appsettings.json file:

{
  "hania": {
    "rabbitmq": {
      "Host": "amqp://guest:guest@rabbitmq:5672",
      "Queue": "test",
      "Namespace": "RMQTest",
      "Exchange": "test",
      "ExchangeType": "fanout",
      "Durable": true,
      "AutoDelete": true
    }
  }
}

The only Required field to set up the client is Host field. The other fields are intended for quantifying client defaults

  • Host : RabbitMQ Server URI Address
  • Queue : Default Queue Name
  • Exchange : Default Exchange Name
  • ExchangeType : Default Exchange Type
  • Durable : Default Durable (true)
  • AutoDelete : Default AutoDelete (false)

2 - Next we need to add our library to Net Core services

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
        
    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddHaniaRabbitMQ();
        
        // or you can set spesific type to get this type assembly
        // exmple: 
        // services.AddHaniaRabbitMQ(typeof(TestConsumer));
    }
}

Consumer

You Should Use the Consumer Attribute to configure the Consumer And Use IConsumer<T> Interface for implementation. (T is a Response Model Type)

And you should write you logic in Handle method.

Important: This library automatically identifies all Consumers and adds them to the project as a service, and there is no need to define Consumers anywhere. You can also easily use Dependencies Injection in your Consumers.

using Hania.NetCore.RabbitMQ.Abstracts;
using Hania.NetCore.RabbitMQ.Attributes;
using Hania.NetCore.RabbitMQ.Sample.Models;
using Hania.NetCore.RabbitMQ.Settings;
using Microsoft.Extensions.Logging;
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Hania.NetCore.RabbitMQ.Sample.Consumers
{
    [Consumer(Exchange = "TopicExchange", Queue = "TopicQueue", BindingKey ="test.*", ExchangeType = ExchangeType.Topic ,AutoDelete =true)]
    public class TestConsumer : IConsumer<TestModel>
    {

        ILogger _logger;

        public TestConsumer(ILogger logger)
        {
            _logger = logger;
        }

        public async Task Handle(TestModel model)
        {
            _logger.Log(LogLevel.Trace,$"Received :  Name is {model.FullName}");
        }
    }
}


DirectConsumer
using Hania.NetCore.RabbitMQ.Abstracts;
using Hania.NetCore.RabbitMQ.Attributes;
using Hania.NetCore.RabbitMQ.Sample.Models;
using Hania.NetCore.RabbitMQ.Settings;
using Microsoft.Extensions.Logging;
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Hania.NetCore.RabbitMQ.Sample.Consumers
{
    [DirectConsumer]
    public class TestDirectConsumer : IConsumer<TestModel>
    {

        public TestDirectConsumer()
        {
        }   

        public async Task Handle(TestModel model)
        {
            Console.WriteLine($"Received from TestDirectConsumer :  Name is {model.FullName}");
        }
    }
}

TopicConsumer
using Hania.NetCore.RabbitMQ.Abstracts;
using Hania.NetCore.RabbitMQ.Attributes;
using Hania.NetCore.RabbitMQ.Sample.Models;
using RabbitMQ.Client;
using System;
using System.Threading.Tasks;

namespace Hania.NetCore.RabbitMQ.Sample.Consumers
{
    [TopicConsumer]
    public class TestTopicConsumer : IConsumer<TestModel>
    {

        public TestTopicConsumer()
        {
        }   

        public async Task Handle(TestModel model)
        {
            Console.WriteLine($"Received From TestTopicConsumer :  Name is {model.FullName}");
        }
    }
}

Producer

Producers in RabbitMq sends event through the client

so , let's publish message :

To test, we write the logic for publishing the message inside the TestController :

important: To send a message, you must inject the IRabbitMQPublisher service into your controller. With IRabbitMQPublisher you can easily send messages to your desired Exchenger and Queue.

using Hania.NetCore.RabbitMQ.Abstracts;
using Hania.NetCore.RabbitMQ.Actions;
using Hania.NetCore.RabbitMQ.Sample.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using RabbitMQ.Client;


namespace Hania.NetCore.RabbitMQ.Sample.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class TestController : ControllerBase
    {
      
        private readonly ILogger<TestController> _logger;
        private readonly IRabbitMQBus _rabbitMQBus;

        public TestController(ILogger<TestController> logger, IRabbitMQBus rabbitMQBus)
        {
            _logger = logger;
            _rabbitMQBus = rabbitMQBus;
        }

        [HttpPost]
        public ActionResult GeneralPost()
        {
            _rabbitMQBus.Publish(new TestModel("akbar ahmadi saray"),"TopicQueue","TopicExchange","test.sayhello",true,true);
            return Ok();
        }

        [HttpPost]
        public async Task<ActionResult> DirectPost()
        {
            await _rabbitMQBus.DirectPublish(new TestModel("akbar ahmadi"));
            return Ok();
        }

        [HttpPost]
        public async Task<ActionResult> TopicPost()
        {
             await _rabbitMQBus.TopicPublish(new TestModel("akbar ahmadi"));
            return Ok();
        }
    }
}

You Can Use this library in your netcore project and Enjoy It 😃)))

Follow me on Social Media :

Linkdin : https://www.linkedin.com/in/akbar-ahmadi-saray-5a5b9016b/ Instagram : https://www.instagram.com/_akbarahmadi_

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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