https://github.com/dcarea/cloudeventdotnet
Publish/Subscribe CloudEvents in .NET, inspired by Dapr Pub/Sub
https://github.com/dcarea/cloudeventdotnet
cloudevents event-driven eventbus kafka microservice microservices-architecture pubsub redis
Last synced: 7 months ago
JSON representation
Publish/Subscribe CloudEvents in .NET, inspired by Dapr Pub/Sub
- Host: GitHub
- URL: https://github.com/dcarea/cloudeventdotnet
- Owner: DCArea
- License: mit
- Created: 2022-07-25T06:13:38.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-07-17T02:14:40.000Z (over 1 year ago)
- Last Synced: 2025-08-21T14:50:31.595Z (7 months ago)
- Topics: cloudevents, event-driven, eventbus, kafka, microservice, microservices-architecture, pubsub, redis
- Language: C#
- Homepage:
- Size: 293 KB
- Stars: 5
- Watchers: 1
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CloudEventDotNet
Publish/Subscribe CloudEvents in .NET, inspired by Dapr Pub/Sub Component
## Features
* Publish/Subscribe events with CloudEvent format
* With both Kafka and Redis support
* At-Least-Once delivery guarantee
* Redeliver failed events
* Obeservability support (traces/metrics)
* ***DOES NOT*** support delivery events in order
## Usage
### Install package
```shell
dotnet add package CloudEventDotNet
dotnet add package CloudEventDotNet.Redis # With Redis Stream
dotnet add package CloudEventDotNet.Kafka # With Apache Kafka
```
### Configure pubsub:
```csharp
services.AddCloudEvents(defaultPubSubName: "kafka", defaultTopic: "my-topic")
.Load(typeof(OrderCancelled).Assembly)
.AddKafkaPubSub("kafka", options =>
{
options.ProducerConfig = new ProducerConfig
{
BootstrapServers = broker,
};
}, options =>
{
options.ConsumerConfig = new ConsumerConfig
{
BootstrapServers = broker,
GroupId = consumerGroup,
};
})
.AddRedisPubSub("redis", options =>
{
options.ConnectionMultiplexerFactory = () => redis;
options.MaxLength = maxLength;
}, options =>
{
options.ConnectionMultiplexerFactory = () => redis;
options.ConsumerGroup = consumerGroup;
})
.AddPubSubDeadLetterSender(opts => // enable dead letter
{
opts.Topic = "DL";
});
```
#### Define cloud event:
```csharp
[CloudEvent] // register event with default pubsub name and topic
public record OrderCancelled(Guid OrderId, string Reason);
```
#### Define cloud event with custom metadata
```csharp
[CloudEvent(PubSubName = "redis", Topic = "another-topic", Type = "a-custom-type")]
public record OrderCancelled(Guid OrderId, string Reason);
```
### Publish a cloud event:
```csharp
var pubsub = serviceProvider.GetRequiredService();
await pubsub.PublishAsync(new OrderCancelled(order.Id, reason));
```
### Subscribe and process cloud event:
``` csharp
public class OrderCancelledHandler : ICloudEventHandler
{
public async Task HandleAsync(CloudEvent cloudEvent, CancellationToken token)
{
// ...
}
}
```
## Performance
The benchmark result on a 4*2.4GHz Core VM:
| | Kafka | Redis |
| --- | --- | --- |
| Publish | ~100k/s | ~90k/s |
| Subscribe | ~150k/s | ~40k/s |
The benchmark code is located at `perf/CloudEventTester`