Skip to content

Hinz3/EasyNetQ

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EasyNetQ Nuget

EasyNetQ auto subscriber dependency implementation

How to use

  1. Add EasyNetQ and MessageDispatcher to your ServiceCollection using:
builder.Services.UseRabbit(builder.Configuration); // Will use appsettings Connection strings looking for Rabbit
builder.Services.UseRabbit("host=localhost;virtualHost=sandbox;username=admin;password=password"); // Put in the connection string directly in the code, instead of using appsettings.
  1. Add AutoSubscriber to Application builder.
app.UseSubscribe(Assembly.GetExecutingAssembly().GetName().Name, Assembly.GetExecutingAssembly());
  1. Add eventhandler/subscriber to service collection.
builder.Services.AddSubscriber<UserUpdatedEventHandler>();

Example

  • Event:
namespace EasyNetQDI.Events
{
    public class UserUpdatedEvent
    {
        public int UserId { get; set; }
    }
}
  • Eventhandler:
using EasyNetQ.AutoSubscribe;
using EasyNetQDI.Events;

namespace EasyNetQDI.SampleAPI
{
    public class UserUpdatedEventHandler : IConsumeAsync<UserUpdatedEvent>
    {
        private readonly ILogger<UserUpdatedEventHandler> logger;

        public UserUpdatedEventHandler(ILogger<UserUpdatedEventHandler> logger)
        {
            this.logger = logger;
        }

        public Task ConsumeAsync(UserUpdatedEvent message, CancellationToken cancellationToken = default)
        {
            logger.LogInformation($"Received: {message.UserId}");
            return Task.CompletedTask;
        }
    }
}
  • Publish event:
using EasyNetQ;
using EasyNetQDI.Events;
using Microsoft.AspNetCore.Mvc;

namespace EasyNetQDI.SampleAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class DemoController : ControllerBase
    {
        private readonly IBus bus;

        public DemoController(IBus bus)
        {
            this.bus = bus;
        }

        [HttpGet]
        public async Task<IActionResult> SendMessage(int userId)
        {
            await bus.PubSub.PublishAsync(new UserUpdatedEvent { UserId = userId }); // Pulish UserUpdatedEvent to RabbitMQ.
            return Ok();
        }
    }
}

About

EasyNetQ auto subscriber dependency implementation

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages