Giter Site home page Giter Site logo

eventbus's Introduction

EventBus

Simple lightweight .NET Event Bus Library using RabbitMQ

Use and Configuration

Publisher

  1. Create an Event in EventBus.InegrationEvent project:
public class MessageEvent : Event
{
    public string Message{ get; set; }
}
  1. Add RabbitMQ configuration in appsetting.json:
  "RabbitMQ": {
    "ConnectionUrl": "amqp://guest:[email protected]:5672/"
  },
  1. Add RabbitMQ.Client Package from nuget

.Net CLI

  dotnet add package RabbitMQ.Client --version 6.4.0

Package Manager Console

  dotnet add package RabbitMQ.Client --version 6.4.0
  1. Add reference EventBus, EventBus.RabbitMQ and EventBus.InegrationEvent in your project
  2. In Program (or StartUp) class add below code to register AddRabbitMQEventBus service
  // In ConfigureServices section
  var rabbitMQSection = builder.Configuration.GetSection("RabbitMQ");
  builder.Services.AddRabbitMQEventBus
  (
      connectionUrl: rabbitMQSection["ConnectionUrl"],
      timeoutBeforeReconnecting: 15
  );
  1. Then in controller or any place that you want to send message use this code snip
  private readonly IEventBus _eventBus;
  
  public HomeController(IEventBus eventBus)
  {
      _eventBus = eventBus;
  }

  [HttpPost]
  public async Task<IActionResult> SendMessage(string message)
  {
    _eventBus.Publish("your exhange name",
    new MessageEvent()
    {
      Message = message ?? string.Empty
    });
    return Ok();
  }

So far we configure publisher (client) and a message sent to specific Exchange. Remember that the create Queue and binding Queue to Exchange done in subscriber (server) part.

Subscriber

  1. Add RabbitMQ configuration in appsetting.json like below
"RabbitMQ": {
  "ConnectionUrl": "amqp://guest:[email protected]:5672/"
},
  1. Add RabbitMQ.Client Package from nuget

.Net CLI

  dotnet add package RabbitMQ.Client --version 6.4.0

Package Manager Console

    dotnet add package RabbitMQ.Client --version 6.4.0
  1. Add referece EventBus, EventBus.RabbitMQ and EventBus.InegrationEvent in your project
  2. Create a handler class to get sent message from publisher
public class MessageEventHandler : IEventHandler<MessageEvent>
{
  private readonly ILogger<MessageEventHandler> _logger;

  public MessageEventHandler(ILogger<MessageEventHandler> logger)
  {
      _logger = logger;
  }

  public Task HandleAsync(MessageEvent @event)
  {
      // Here you handle what happens when you receive an event of this type from the event bus.

      _logger.LogInformation(@event.ToString());
      return Task.CompletedTask;
  }
}
  1. In Program (or StartUp) class add below code to register AddRabbitMQEventBus service and setup subscriber
  // In ConfigureServices section
  var rabbitMQSection = builder.Configuration.GetSection("RabbitMQ");
  builder.Services.AddRabbitMQEventBus
  (
      connectionUrl: rabbitMQSection["ConnectionUrl"],
      timeoutBeforeReconnecting: 15
  );
  builder.Services.AddTransient<MessageEventHandler>();
  
  // In Configure section
  var eventBus = app.Services.GetRequiredService<IEventBus>();

  eventBus.Subscribe<MessageEvent, MessageEventHandler>(
      your exhange name,
      your Queue name);

According to above snip code we configure MessageEventHandler subscriber to subcribing message from MessageEvent that send before from publisher. Also remember, in my example in this repository the Publisher and Subscriber are in the same project

eventbus's People

Contributors

majidgholipour avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.