RedisMessenger 0.1.2

dotnet add package RedisMessenger --version 0.1.2
                    
NuGet\Install-Package RedisMessenger -Version 0.1.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="RedisMessenger" Version="0.1.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RedisMessenger" Version="0.1.2" />
                    
Directory.Packages.props
<PackageReference Include="RedisMessenger" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RedisMessenger --version 0.1.2
                    
#r "nuget: RedisMessenger, 0.1.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.
#:package RedisMessenger@0.1.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RedisMessenger&version=0.1.2
                    
Install as a Cake Addin
#tool nuget:?package=RedisMessenger&version=0.1.2
                    
Install as a Cake Tool

RedisMessenger .NET Client

The RedisMessenger .NET Client allows you to use Redis as a message broker to communicate between applications that implement the RedisMessenger protocol.

Install

You can install this library via NuGet:

dotnet add package RedisMessenger

Usage

RedisMessenger can be used either on its own or as an injectable service.

Standalone

If you don't use a hosted application or dependency injection container:

IRedisMessenger messenger = RedisMessenger.Create(configure =>
{
    configure.RedisConfiguration = "localhost";
    configure.ClientName = "my-messaging-client";
    configure.ChannelPrefix = "cool-zone";

    configure.RedisConfigure = redisOpts =>
    {
        redisOpts.AbortOnConnectFail = false;
    };

    configure.AddMessageHandler<MyMessageHandler>("my-message-channel");
});

DI container

If you want it to be injectable:

builder.Services.AddRedisMessenger(configure => ConfigureTheMessengerLikeAbove(configure));

Messages

Woooo, now let's send some messages or something:

var channel = messenger.GetMessageChannel<MyMessageRequest, MyMessageResponse>("my-message-channel");
await channel.SendAsync(new MyMessageRequest()); // Fire and forget, just like walking away from an explosion

try
{
    MyMessageResponse? res = await channel.QueryAsync(new MyMessageRequest());
    Console.WriteLine($"Knock knock, you got a message {res}");
}
catch (RedisMessengerResponseException ex)
{
    Console.WriteLine($"Woopsies, something failed on the handler side :( {ex.Message}");
}
catch (OperationCanceledException)
{
    Console.WriteLine("Probably worked on your machine but this is the cloud");
}

Message handlers

In the .NET library, you can have both typed and raw message handlers:

class MyRawMessageHandler : RedisMessenger.MessageHandler
{
    protected override async Task<object?> HandleMessageAsync(System.Text.Json.JsonElement? payload)
    {
        // Exceptions will be reported to the message sender.
        return await DoSomethingWithPayloadAsync(payload);
    }
}

class MyMessageHandler : RedisMessenger.MessageHandler<RequestType, ResponseType>
{
    private readonly ILogger _logger;
    public MyMessageHandler(ILogger<MyMessageHandler> logger) => _logger = logger;

    protected override async Task<ResponseType> HandleMessageAsync(RequestType? payload)
    {
        // JSON deserialization errors and exceptions will be reported to the message sender.
        return await DoSomethingWithPayloadAsync(payload);
    }
}

You can only register one message handler per channel.

Dependency injection

As you can see, other services can be injected into your message handlers. Each message has its own scope just like ASP.NET Core HTTP requests.

Redis configuration

This library is built upon StackExchange.Redis and allows you to configure the Redis connection as you wish via RedisMessengerConfiguration.RedisConfigure and RedisMessengerConfiguration.RedisConfiguration.

License

MIT. Have fun.

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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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
0.1.2 151 3/26/2024
0.1.1 134 3/21/2024
0.1.0 113 3/21/2024