SimpleKafkaLibrary 1.0.5
dotnet add package SimpleKafkaLibrary --version 1.0.5
NuGet\Install-Package SimpleKafkaLibrary -Version 1.0.5
<PackageReference Include="SimpleKafkaLibrary" Version="1.0.5" />
paket add SimpleKafkaLibrary --version 1.0.5
#r "nuget: SimpleKafkaLibrary, 1.0.5"
// 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 | Versions 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. |
-
net8.0
- Confluent.Kafka (>= 2.4.0)
- Microsoft.Extensions.Configuration (>= 8.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 8.0.1)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Options (>= 8.0.2)
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.