Unitee.EventDriven.Abstraction 8.0.0-alpha.2

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

// Install Unitee.EventDriven.Abstraction as a Cake Tool
#tool nuget:?package=Unitee.EventDriven.Abstraction&version=8.0.0-alpha.2&prerelease

Unitee.EventDriven

https://github.com/uniteeio/Unitee.EventDriven

logo

Summary

Unitee.EventDriven is library to deal with Event Driven Programming (EDP) in a distributed environment.

Build Nuget

dotnet add package Unitee.EventDriven.RedisStream

For now, we mainly focus on Redis as an event store because:

  • Easy to deploy or find free (cheap) clusters
  • Easy to visualize with a gui tool
  • A tool you may already familiar with (for caching for example)
  • Builtin system for pub/sub and storing streams
  • Good .NET integration

Features

  • Publishing distributed messages
  • Subscribe to distributed messages
  • Request/Reply pattern
  • Scheduling messages
  • Treat pending messages at start

How to use

  1. Use the package StackExchang.Redis to make the IConnectionMultiplexer in the DI container.
var multiplexer = ConnectionMultiplexer.Connect(builder.Configuration["Redis:ConnectionString"]);
builder.Services.AddSingleton<IConnectionMultiplexer>(multiplexer);
  1. Create an event as a POCO object.
[Subject("USER_REGISTERED")]
public record UserRegistered(int UserId, string Email);

If the subject is ommited, the name of the object is used instead (here, UserRegistered)

Guide

Publish an event

Setup

builder.Services.AddScoped<IRedisStreamPublisher, RedisStreamPublisher>();

Publish

Use the IRedisStreamPublisher to actually publish the event:

[ApiController]
public class UserController : ControllerBase
{

    private readonly IRedisStreamPublisher _publisher;
    private readonly IUserService _userService;

    public UserController(IRedisStreamPublisher publisher, IUserService userService)
    {
        _publisher = publisher;
        _userService = userService;
    }

    public async Task<IActionResult> Register(string email)
    {
        var userId = _userService.CreateUserInBdd();

        await _publisher.PublishAsync(new UserRegistered(userId, email));

        return Ok();
    }

    // Request a reply
    public async Task<IActionResult> ForgotPassword(string email)
    {
        try
        {
            var response = await _publisher.RequestResponseAsync(new PasswordForgotten(email));
            return Ok();
        } 
        catch (TimeoutException)
        {
            return NotFound();
        }
    }

    // Schedule
    public async Task<IActionResult> Register(string email)
    {
        await _publisher.PublishAsync(new UserRegistered30MinutesAgo(email), new()
        {
            ScheduledEnqueueTime = DateTime.UtcNow.AddMinutes(30);
        });

        return Ok();
    }
}

Consume an event

Setup

You need to register a RedisStreamBackgorundReceiver:

services.AddRedisStreamBackgroundReceiver("ConsumerService");

Implementation detail: The name is used to create consumer groups. A message is delivered to all the consumer groups. (one to many communication).

Consume

You also need to create a class that implements: IRedisStreamConsumer<TEvent>

public class UserRegisteredConsumer : IRedisStreamConsumer<UserRegistered>
{
    public async Task ConsumeAsync(UserRegistered message)
    {
        await _email.Send(message.Email);
    }
}

Then, register your consumer:

services.AddTransient<IConsumer, UserRegisteredConsumer>();

All consumers should be added using AddTransient. So, they all have their own scope since they are executed concurrently.

If you want to your consumer to be able to reply, then, implement IRedisStreamConsumerWithContext<TRequest, TResponse> instead.

public class UserRegisteredConsumer : IRedisStreamConsumeWithContext<UserRegistered, MyResponse>
{
    public async Task ConsumeAsync(UserRegistered message, IRedisStreamMessageContext context)
    {
        await _email.Send(message.Email);

        await context.ReplyAsync(new MyResponse());
    }
}

Dead letter queue

If a consumer throw, then the message and the exception are published to a special queue named: dead letter queue. The default name is DEAD_LETTER but you can configured it by providing a second parameter to AddRedisStreamBackgroundReceiver. You can easily imagine a script able to pull the messages from the dead letter queue and send them again.

Horizontal scaling

Inside a consumer group, you can have multiple consumers. Each consumer group receives a single copy of the message. You can name the consumer with the third parameter of AddRedisStreamBackgroundReceiver. You should use an unique name PER INSTANCE.

Thread safety and concurrency

When multiple consumer are subscribed to the same event, or when, there is multiple event pending, they are executed concurrently. This mean that you should not rely of the order they are inserted.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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 (2)

Showing the top 2 NuGet packages that depend on Unitee.EventDriven.Abstraction:

Package Downloads
Unitee.EventDriven.RedisStream

Light abstraction and good practices for Service Bus at Unitee

Unitee.EventDriven.AzureServiceBus

Light abstraction and good practices for Service Bus at Unitee

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
11.0.0 1,174 8/31/2023
10.2.0 245 7/10/2023
10.0.0 194 7/7/2023
9.2.0 252 6/29/2023
9.1.0-alpha.1 349 4/24/2023
9.0.0-alpha.4 140 3/9/2023
9.0.0-alpha.2 87 3/9/2023
9.0.0-alpha.1 177 2/21/2023
8.0.0-alpha.2 107 2/16/2023
8.0.0-alpha.1 90 2/15/2023
7.0.0 308 2/15/2023
7.0.0-alpha.1 339 2/7/2023
6.1.1 531 12/12/2022
6.1.0 372 12/8/2022
6.0.3 431 11/28/2022
6.0.2 369 11/28/2022
5.6.0 764 11/8/2022
5.5.0 500 11/8/2022
5.4.0 493 11/8/2022
5.3.0 498 11/8/2022
5.2.0 520 11/8/2022
5.1.0 492 11/8/2022
5.0.1 908 10/27/2022