Giter VIP home page Giter VIP logo

redismessenger-dotnet's Introduction

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.

redismessenger-dotnet's People

Contributors

danmitreanu avatar

Stargazers

 avatar Rares Modure avatar

Watchers

 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.