SimpleKafkaLibrary 1.0.5

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

// Install SimpleKafkaLibrary as a Cake Tool
#tool nuget:?package=SimpleKafkaLibrary&version=1.0.5                

Kafka Implementation

in appsettings.json put your configuration

Configuration:

"Kafka": {
    "bootstrapservers": "localhost:9092",
    "UseKafka": true,
    "UseEncryptedData":false,
    "EncryptedKey":"xxxxxx" 
    "FlushProducerInSeconds": 2,
    "ConsumedInSeconds": 2
}

Note that you can use Confluent.Kafka configuration and EncryptedKey is optional if you want to encrypt the data

Producer Usage Sample Producer DI

 public class CreateUserCommandHandler : IRequestHandler<CreateUserCommand, PayloadResponse<UsersDto>>
 {
    private readonly IMessageProducers _messageProducers;

    public CreateUserCommandHandler(IMessageProducers messageProducers)
    {
       _messageProducers = messageProducers
    }

    public async Task<PayloadResponse<UsersDto>> Handle(CreateUserCommand request, CancellationToken cancellationToken)
        {
            ...

            await _messageProducers.ProduceAsync<UsersDto>("User", newUser).ConfigureAwait(false);

            return ResponseStatus<UsersDto>.Create<PayloadResponse<UsersDto>>(ResponseCodes.SUCCESSFUL, _messageProvider.GetMessage(ResponseCodes.SUCCESSFUL), newUser);
        }

 }

Consumer Usage

Sample Consumer class

Path: src\Core\SmartCleanArchitecture.Application\kafkaConsumer\Consumer.cs

    public class Consumer : ConsumerBase
    {

        public Consumer(KafkaConfig configuration, IMessageProducers producers, IMessageAdmin messageAdmin) : base("test1", configuration, messageAdmin)
        {

        }

        public override async Task Invoke()
        {

            await ConsumeAsync<string>("testTopic", (value) =>
            {  
                // put your action here
                Console.WriteLine(value);
            });

            await base.Invoke();
        }
    }

Note: - You can create multiple Consumer classes and configure them in the configuration file. - The groupId is test1. - The topic to be consumed is testTopic.

services.AddKafkaServices<Consumer>(kafkaConfig);

OR


services.AddKafkaServices(cfg =>
{
    cfg.Configure(configuration.GetSection("Kafka"));
    cfg.RegisterConsumer<Consumer>();
    cfg.RegisterConsumer<Consumer2>(); 
});

OR



services.AddKafkaServices(cfg =>
{
    cfg.Configure(configuration.GetSection("Kafka"));

    cfg.RegisterConsumer(Assembly.GetExecutingAssembly());

});

License

This project is licensed with the MIT license.

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. 
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.5 84 1/20/2025
1.0.4 113 12/27/2024
1.0.3 102 12/17/2024
1.0.2 154 11/27/2024
1.0.1 118 6/22/2024
1.0.0 112 6/22/2024