M.EventBrokerSlim 3.2.0

dotnet add package M.EventBrokerSlim --version 3.2.0                
NuGet\Install-Package M.EventBrokerSlim -Version 3.2.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="M.EventBrokerSlim" Version="3.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add M.EventBrokerSlim --version 3.2.0                
#r "nuget: M.EventBrokerSlim, 3.2.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 M.EventBrokerSlim as a Cake Addin
#addin nuget:?package=M.EventBrokerSlim&version=3.2.0

// Install M.EventBrokerSlim as a Cake Tool
#tool nuget:?package=M.EventBrokerSlim&version=3.2.0                

EventBrokerSlim

An implementation of broadcasting events in a fire-and-forget style.

Features:

  • in-memory, in-process
  • publishing is Fire and Forget style
  • events don't have to implement specific interface
  • event handlers are executed on a ThreadPool threads
  • the number of concurrent handlers running can be limited
  • built-in retry option
  • tightly integrated with Microsoft.Extensions.DependencyInjection
  • each handler is resolved and executed in a new DI container scope
  • NEW event handlers can be delegates
  • NEW dynamic delegate event handlers

How does it work

Implement an event handler by implementing IEventHandler<TEvent> interface:

public record SomeEvent(string Message);

public class SomeEventHandler : IEventHandler<SomeEvent>
{
    // Inject services added to the DI container
    public SomeEventHandler()
    {
    }

    public async Task Handle(SomeEvent @event, IRetryPolicy retryPolicy, CancellationToken cancellationToken)
    {
        // process the event
    }

    public async Task OnError(Exception exception, SomeEvent @event, IRetryPolicy retryPolicy, CancellationToken cancellationToken)
    {
        // called on unhandled exception from Handle 
        // optionally use retryPolicy.RetryAfter(TimeSpan)
    }
}

or use DelegateHandlerRegistryBuilder to register delegate as handler:

DelegateHandlerRegistryBuilder builder = new();
builder.RegisterHandler<SomeEvent>(
    static async (SomeEvent someEvent, ISomeService service, CancellationToken cancellationToken) =>
    {
        await service.DoSomething(someEvent, cancellationToken);
    });

Add event broker implementation to DI container using AddEventBroker extension method and register handlers, optionally add delegate handler registries:

serviceCollection.AddEventBroker(x => x.AddTransient<SomeEvent, SomeEventHandler>())
                 .AddSingleton(builder);

Inject IEventBroker and publish events:

class MyClass
{
    private readonly IEventBroker _eventBroker;

    public MyClass(IEventBroker eventBroker)
    {
        _eventBroker = eventBroker;
    }
    
    public async Task DoSomething()
    {
        var someEvent = new SomeEvent("Something happened");
        await _eventBroker.Publish(someEvent);
    }
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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
3.2.0 407 9/14/2024
3.1.0 521 6/29/2024
3.0.0 386 5/2/2024
2.0.0 107 5/1/2024
1.0.0 650 1/12/2024
1.0.0-preview3 108 1/6/2024
1.0.0-preview2 130 1/1/2024
1.0.0-preview1 119 12/28/2023