FluentKafkaProducer 1.0.0

dotnet add package FluentKafkaProducer --version 1.0.0
                    
NuGet\Install-Package FluentKafkaProducer -Version 1.0.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="FluentKafkaProducer" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FluentKafkaProducer" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="FluentKafkaProducer" />
                    
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 FluentKafkaProducer --version 1.0.0
                    
#r "nuget: FluentKafkaProducer, 1.0.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.
#:package FluentKafkaProducer@1.0.0
                    
#: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=FluentKafkaProducer&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=FluentKafkaProducer&version=1.0.0
                    
Install as a Cake Tool

FluentKafkaProducer

This package provides a robust, extensible Kafka producer client for .NET applications. It includes serialization support for multiple formats (e.g., JSON, MessagePack), observability via OpenTelemetry metrics and activities, and a clean DI-friendly architecture.

✨ Features

  • ✅ Plug-and-play integration with Kafka using Confluent's client

  • ✅ Built-in support for JSON and MessagePack serialization

  • ✅ Easily extendable with custom serializers

  • ✅ OpenTelemetry-based metrics and tracing instrumentation


📦 Installation

Install

Install via NuGet:

dotnet add package FluentKafkaProducer

Or via the NuGet Package Manager:

Install-Package FluentKafkaProducer

🛠️ Usage

Register the Producer

    services.AddFluentKafkaProducerServices(new KafkaProducerSettingsDto
    {
        Hosts = "localhost:9092",
        UserName = "your-user",
        Password = "your-password",
        SaslMechanism = SaslMechanism.Plain,
        SecurityProtocol = SecurityProtocol.SaslPlaintext
    });

Producing messages

Inject IKafkaProducerService and send messages easily:

public class SomeService
{
    private readonly IKafkaProducerService _kafkaProducer;

    public SomeService(IKafkaProducerService kafkaProducer)
    {
        _kafkaProducer = kafkaProducer;
    }

    public async Task PublishAsync()
    {
        var message = new MyEvent { Id = 1, Name = "test" };
        await _kafkaProducer.SendToKafka(message, "my-topic", key: "event-1", formatKey: KafkaFormatKeys.Json);
    }
}

⚙️ KafkaProducerSettings


public sealed record KafkaProducerSettings
{
    public string Hosts { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public SecurityProtocol SecurityProtocol { get; set; } = SecurityProtocol.SaslPlaintext;
    public SaslMechanism SaslMechanism { get; set; } = SaslMechanism.Plain;
}

Serialization

Two built-in serializers are included:

JsonKafkaMessageSerializer

MessagePackKafkaMessageSerializer

You can add your own by implementing:

public interface IKafkaMessageSerializer
{
    string FormatKey { get; }
    Headers Headers { get; }
    byte[] Serialize<T>(T data) where T : class;
}

Then register it:

services.AddSingleton<IKafkaMessageSerializer, CustomKafkaMessageSerializer>();

Use it when producing:

await _kafkaProducer.SendToKafka(message, "my-topic", key: "event-1", formatKey: "CustomFormatKey");

📊 Observability

OpenTelemetry instrumentation includes:

  • ActivitySource: Tracing message send operations

  • Metrics:

    • kafka.messages.sent

    • kafka.messages.failed

    • kafka.message.send.duration (ms)

Tracing includes:

  • Kafka topic

  • Message key

  • Exceptions and status


builder.Services.AddOpenTelemetry()
    .WithTracing(b => b.AddKafkaProducerInstrumentation())
    .WithMetrics(b => b.AddKafkaProducerInstrumentation());

📌 Notes

  • Message serialization is format-key based, defaulting to JSON.

  • serializers are resolved by key; you can plug in any format.

  • OpenTelemetry integration is optional but strongly recommended.

📜 License

MIT — Feel free to use and modify.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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
1.0.0 259 5/9/2025