EasyCore.Kafka
8.3.1
dotnet add package EasyCore.Kafka --version 8.3.1
NuGet\Install-Package EasyCore.Kafka -Version 8.3.1
<PackageReference Include="EasyCore.Kafka" Version="8.3.1" />
<PackageVersion Include="EasyCore.Kafka" Version="8.3.1" />
<PackageReference Include="EasyCore.Kafka" />
paket add EasyCore.Kafka --version 8.3.1
#r "nuget: EasyCore.Kafka, 8.3.1"
#:package EasyCore.Kafka@8.3.1
#addin nuget:?package=EasyCore.Kafka&version=8.3.1
#tool nuget:?package=EasyCore.Kafka&version=8.3.1
π¨ EasyCore.Kafka
EasyCore.Kafka is a Kafka infrastructure client for .NET 8. Built on Confluent.Kafka, it provides DI registration, connect, topic publish/subscribe, and manual Commit. It is not tied to
IEvent/ EventBus β use it standalone, or via theEasyCore.EventBus.Kafkaadapter.
π Language
- Chinese: README.md
- English (this document)
π Table of Contents
- 1. Positioning
- 2. Relation to EventBus
- 3. Requirements
- 4. Installation
- 5. Quick Start
- 6. API Overview
- 7. Options (
KafkaOptions) - 8. FAQ
- 9. License
1. π― Positioning
| Scenario | Fit? |
|---|---|
| Byte-level publish/subscribe on Kafka topics | β |
| Key, headers, and manual Commit | β |
Unified IEvent + handler discovery (EDA) |
β β use EasyCore.EventBus.Kafka |
| In-process local events | β β use EasyCore.EventBus local bus |
1.1 Design Principles
| Principle | Meaning |
|---|---|
| Infrastructure layer | Bootstrap, topics, consumer groups, and raw bytes |
| Standalone | No dependency on the EasyCore.EventBus core package |
| Adapter-friendly | The EventBus Kafka adapter reuses this client |
2. Relation to EventBus
βββββββββββββββββββββββββββββββ
β App code (any payload) β
ββββββββββββββββ¬βββββββββββββββ
β IKafkaClient
βΌ
βββββββββββββββββββββββββββββββ
β EasyCore.Kafka (this) β β infrastructure client
ββββββββββββββββ¬βββββββββββββββ
β Confluent.Kafka
βΌ
Kafka Cluster
βββββββββββββββββββββββββββββββ
β EasyCore.EventBus.Kafka β β adapter: IEvent / Handler
β βββ uses this client β
βββββββββββββββββββββββββββββββ
| Package | Role |
|---|---|
EasyCore.Kafka |
Generic Kafka client (this package) |
EasyCore.EventBus.Kafka |
Maps EventBus IEvent onto Kafka |
3. β Requirements
| Item | Requirement |
|---|---|
| .NET | 8.0+ |
| Dependency | Confluent.Kafka 2.12.x (brought by this package) |
| Broker | Reachable Kafka bootstrap servers |
4. π₯ Installation
dotnet add package EasyCore.Kafka
5. β‘ Quick Start
5οΈβ£.1οΈβ£ Register DI
using EasyCore.Kafka;
builder.Services.AddEasyCoreKafka(o =>
{
o.BootstrapServers = "localhost:9092";
o.TopicName = "EasyCore.Topic";
o.GroupId = "EasyCore.GroupId";
o.MessageTimeoutMs = 10000;
o.RequestTimeoutMs = 10000;
});
5οΈβ£.2οΈβ£ Publish
public sealed class TelemetryPublisher
{
private readonly IKafkaClient _client;
public TelemetryPublisher(IKafkaClient client) => _client = client;
public async Task PublishAsync(string deviceId, CancellationToken ct = default)
{
await _client.ConnectAsync(ct);
var body = Encoding.UTF8.GetBytes($$"""{"deviceId":"{{deviceId}}"}""");
var headers = new Dictionary<string, byte[]>
{
["x-source"] = Encoding.UTF8.GetBytes("iot-gateway")
};
await _client.PublishAsync(
topic: "telemetry.raw",
body: body,
key: deviceId,
headers: headers,
cancellationToken: ct);
}
}
5οΈβ£.3οΈβ£ Subscribe and Commit
public sealed class TelemetryConsumer : BackgroundService
{
private readonly IKafkaClient _client;
public TelemetryConsumer(IKafkaClient client) => _client = client;
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await _client.ConnectAsync(stoppingToken);
await _client.SubscribeAsync(
topics: new[] { "telemetry.raw" },
handler: async (msg, ct) =>
{
var json = Encoding.UTF8.GetString(msg.Body.Span);
// handle businessβ¦
if (msg.NativeResult is not null)
_client.Commit(msg.NativeResult);
await Task.CompletedTask;
},
cancellationToken: stoppingToken);
}
}
KafkaDeliveredMessage fields: Topic, Key, Body, Headers, NativeResult (pass to Commit).
6. π§© API Overview
| Member | Description |
|---|---|
AddEasyCoreKafka(Action<KafkaOptions>) |
DI: Options and IKafkaClient |
ConnectAsync |
Initialize the consumer (idempotent) |
PublishAsync(topic, body, key?, headers?) |
Produce to a topic |
SubscribeAsync(topics, handler) |
Subscribe and start a background consume loop |
Commit(nativeResult) |
Commit the offset for the delivered message |
IKafkaClient implements IAsyncDisposable β dispose on shutdown.
7. Options (KafkaOptions)
| Property | Type | Default | Description |
|---|---|---|---|
BootstrapServers |
string |
localhost:9092 |
Comma-separated bootstrap addresses |
TopicName |
string |
EasyCore.Topic |
Topic name prefix (convention / adapter) |
GroupId |
string |
EasyCore.GroupId |
Consumer group suffix |
MessageTimeoutMs |
int |
10000 |
Produce timeout (ms) |
RequestTimeoutMs |
int |
10000 |
Request timeout (ms) |
QueueBufferingMaxMessages |
int |
30000 |
Producer queue buffer size |
AppName |
string? |
null |
Used in group naming; entry assembly when null |
8. β FAQ
Q: This package vs EasyCore.EventBus.Kafka?
A: Use the EventBus adapter for IEvent, handlers, and retries. Use this package for raw topic I/O.
Q: What do I pass to Commit?
A: Pass KafkaDeliveredMessage.NativeResult from the handler (the underlying consume result).
Q: What do TopicName / GroupId do when used standalone?
A: Naming conventions / suffixes. PublishAsync / SubscribeAsync still use the topics you pass in.
Q: Multiple bootstrap servers?
A: Yes β set BootstrapServers to e.g. kafka1:9092,kafka2:9092.
9. π License
MIT β see the repository LICENSE, or the NuGet package metadata.
π€ Contributing
Issues and PRs are welcome. After changing this package, verify EasyCore.EventBus.Kafka and related demos.
| 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. 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. |
-
net8.0
- Confluent.Kafka (>= 2.12.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options (>= 8.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on EasyCore.Kafka:
| Package | Downloads |
|---|---|
|
EasyCore.EventBus.Kafka
.NET Core EventBus distributed transport based on Kafka. |
GitHub repositories
This package is not used by any popular GitHub repositories.