Skip to content

Commit 69cbbfe

Browse files
Fixed Kafka article to use consistent connection names. (#4197)
1 parent ac55f7a commit 69cbbfe

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

docs/messaging/kafka-integration.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,12 @@ dotnet add package Aspire.Confluent.Kafka
161161
In the _:::no-loc text="Program.cs":::_ file of your client-consuming project, call the <xref:Microsoft.Extensions.Hosting.AspireKafkaProducerExtensions.AddKafkaProducer%2A> extension method to register an `IProducer<TKey, TValue>` for use via the dependency injection container. The method takes two generic parameters corresponding to the type of the key and the type of the message to send to the broker. These generic parameters are used by `AddKafkaProducer` to create an instance of `ProducerBuilder<TKey, TValue>`. This method also takes connection name parameter.
162162

163163
```csharp
164-
builder.AddKafkaProducer<string, string>("messaging");
164+
builder.AddKafkaProducer<string, string>("kafka");
165165
```
166166

167+
> [!TIP]
168+
> The `connectionName` parameter must match the name used when adding the Kafka resource in the AppHost project. In other words, when you call `AddKafka` and provide a name of `kafka` that same name should be used when calling `AddKafkaProducer`. For more information, see [Add Kafka server resource](#add-kafka-server-resource).
169+
167170
You can then retrieve the `IProducer<TKey, TValue>` instance using dependency injection. For example, to retrieve the producer from an `IHostedService`:
168171

169172
```csharp
@@ -180,9 +183,12 @@ For more information on workers, see [Worker services in .NET](/dotnet/core/exte
180183
To register an `IConsumer<TKey, TValue>` for use via the dependency injection container, call the <xref:Microsoft.Extensions.Hosting.AspireKafkaConsumerExtensions.AddKafkaConsumer%2A> extension method in the _:::no-loc text="Program.cs":::_ file of your client-consuming project. The method takes two generic parameters corresponding to the type of the key and the type of the message to receive from the broker. These generic parameters are used by `AddKafkaConsumer` to create an instance of `ConsumerBuilder<TKey, TValue>`. This method also takes connection name parameter.
181184

182185
```csharp
183-
builder.AddKafkaConsumer<string, string>("messaging");
186+
builder.AddKafkaConsumer<string, string>("kafka");
184187
```
185188

189+
> [!TIP]
190+
> The `connectionName` parameter must match the name used when adding the Kafka resource in the AppHost project. In other words, when you call `AddKafka` and provide a name of `kafka` that same name should be used when calling `AddKafkaComsumer`. For more information, see [Add Kafka server resource](#add-kafka-server-resource).
191+
186192
You can then retrieve the `IConsumer<TKey, TValue>` instance using dependency injection. For example, to retrieve the consumer from an `IHostedService`:
187193

188194
```csharp
@@ -210,15 +216,15 @@ The .NET Aspire Apache Kafka integration provides multiple options to configure
210216
When using a connection string from the `ConnectionStrings` configuration section, you can provide the name of the connection string when calling `builder.AddKafkaProducer()` or `builder.AddKafkaProducer()`:
211217

212218
```csharp
213-
builder.AddKafkaProducer<string, string>("kafka-producer");
219+
builder.AddKafkaProducer<string, string>("kafka");
214220
```
215221

216222
Then the connection string is retrieved from the `ConnectionStrings` configuration section:
217223

218224
```json
219225
{
220226
"ConnectionStrings": {
221-
"kafka-producer": "broker:9092"
227+
"kafka": "broker:9092"
222228
}
223229
}
224230
```
@@ -300,15 +306,15 @@ You can pass the `Action<KafkaProducerSettings> configureSettings` delegate to s
300306

301307
```csharp
302308
builder.AddKafkaProducer<string, string>(
303-
"messaging",
309+
"kafka",
304310
static settings => settings.DisableHealthChecks = true);
305311
```
306312

307313
You can configure inline a consumer from code:
308314

309315
```csharp
310316
builder.AddKafkaConsumer<string, string>(
311-
"messaging",
317+
"kafka",
312318
static settings => settings.DisableHealthChecks = true);
313319
```
314320

@@ -318,7 +324,7 @@ To configure `Confluent.Kafka` builders, pass an `Action<ProducerBuilder<TKey, T
318324

319325
```csharp
320326
builder.AddKafkaProducer<string, MyMessage>(
321-
"messaging",
327+
"kafka",
322328
static producerBuilder =>
323329
{
324330
var messageSerializer = new MyMessageSerializer();
@@ -335,7 +341,7 @@ Consider the following producer registration example:
335341

336342
```csharp
337343
builder.AddKafkaProducer<string, MyMessage>(
338-
"messaging",
344+
"kafka",
339345
static (serviceProvider, producerBuilder) =>
340346
{
341347
var messageSerializer = serviceProvider.GetRequiredServices<MyMessageSerializer>();

0 commit comments

Comments
 (0)