AddKeyedKafkaConsumer/Producer: have separate argument for name (serviceKey) and connection name
- Dominant language
- C#
- Stars
- 6.3k
- Forks
- 991
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 196
Description
## Background and Motivation
While playing with Aspire, I encountered a situation where I needed multiple Kafka consumers each with a different `groupId` but I needed them all to connect to the same cluster and subscribe to the same topic.
For that, I used the `AddKeyedKafkaConsumer` extension. The problem I experienced was that the service key was also used as the connection name thus failing to connect to the cluster defined in the AppHost.
## Proposed API
```diff
- public static void AddKeyedKafkaConsumer(this IHostApplicationBuilder builder, string name)
+ public static void AddKeyedKafkaConsumer(this IHostApplicationBuilder builder, string serviceKey, string connectionName)
```
## Usage Example
### Current API with a workaround - Using another override but with the same idea
```csharp
builder.AddKeyedKafkaConsumer("serviceA", (KafkaConsumerSettings settings) =>
{
options.ConnectionString = conf.GetConnectionString(connectionName);
});
```
### Usage of proposed API
```csharp
builder.AddKeyedKafkaConsumer("serviceA", connectionName);
```
## Alternative Designs
```csharp
public static void AddKeyedKafkaConsumer(this IHostApplicationBuilder builder, string serviceKey, string connectionName)
{
ArgumentException.ThrowIfNullOrEmpty(name);
AddKafkaConsumerInternal(builder, null, null, connectionName: connectionName, serviceKey: serviceKey);
}
```
Contributor guide
Assessment
This issue has not been assessed yet.