gsferreira / kafkaflow-mediatr

An Extension to KafkaFlow that adds MediatR as a Middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

KafkaFlow.MediatR

Introduction

An Extension to KafkaFlow that adds MediatR as a Middleware.

You can find samples here.

Installation

Using .NET CLI:

dotnet add package gsferreira.KafkaFlow.MediatR

Using NuGet:

Install-Package gsferreira.KafkaFlow.MediatR

Documentation

You can forward messages as Requests or Notifications to Mediator. To do it, chain one of the middlewares provided by this library after a Serializer Middleware. The serializer should be able to deserialize the incoming message into a message that implements MediatR IRequest or INotification.

Publishing Notifications

To publish messages as notifications to Mediator, use the middleware AddMediatorNotifications().

services.AddKafka(kafka =>
    kafka
        .AddCluster(cluster => cluster
            .WithBrokers(new[] { "localhost:9092" })
            .AddConsumer(consumer => consumer
                .Topic("sample-topic")
                .WithGroupId("sample-group")
                .WithBufferSize(100)
                .WithWorkersCount(10)
                .AddMiddlewares(middlewares => middlewares
                    .AddSerializer<JsonCoreSerializer>()
                    .AddMediatorNotifications()
                )
            )
        ));

Publishing Requests

To publish messages as requests to Mediator, use the middleware AddMediatorRequests().

services.AddKafka(kafka =>
    kafka
        .AddCluster(cluster => cluster
            .WithBrokers(new[] { "localhost:9092" })
            .AddConsumer(consumer => consumer
                .Topic("sample-topic")
                .WithGroupId("sample-group")
                .WithBufferSize(100)
                .WithWorkersCount(10)
                .AddMiddlewares(middlewares => middlewares
                    .AddSerializer<JsonCoreSerializer>()
                    .AddMediatorRequests()
                )
            )
        ));

Controlling lifetime

The default KafkaFlow Middleware lifetime isn't compatible with the default MediatR Handler lifetime. You need to align those either through the Middleware overload AddMediatorNotifications(lifetime)/AddMediatorNotifications(lifetime) or through MediatR defaults configuration:

services.AddMediatR(cfg =>
{
    cfg.Lifetime = ServiceLifetime.Singleton;
});

Get in touch

License

KafkaFlow.MediatR is a free and open source project, released under the permissible MIT license.

About

An Extension to KafkaFlow that adds MediatR as a Middleware

License:MIT License


Languages

Language:C# 100.0%