# KafkaBuilderExtensions Methods

- Package: [Aspire.Hosting.Kafka](/reference/api/csharp/aspire.hosting.kafka.md)
- Type: [KafkaBuilderExtensions](/reference/api/csharp/aspire.hosting.kafka/kafkabuilderextensions.md)
- Kind: `Methods`
- Members: `5`

Provides extension methods for adding Kafka resources to the application model.

## AddKafka(IDistributedApplicationBuilder, string, int?)

- Name: `AddKafka(IDistributedApplicationBuilder, string, int?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<KafkaServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Kafka/KafkaBuilderExtensions.cs#L36-L80)

Adds a Kafka resource to the application. A container is used for local development.

```csharp
public static class KafkaBuilderExtensions
{
    public static IResourceBuilder<KafkaServerResource> AddKafka(
        this IDistributedApplicationBuilder builder,
        string name,
        int? port = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IDistributedApplicationBuilder`)
  The `Hosting.IDistributedApplicationBuilder`.
- `name` (`string`)
  The name of the resource. This name will be used as the connection string name when referenced in a dependency
- `port` (`int?`) `optional`
  The host port of Kafka broker.

## Returns

`IResourceBuilder<KafkaServerResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## Remarks

This version of the package defaults to the tag of the container image.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithDataBindMount(IResourceBuilder<KafkaServerResource>, string, bool)

- Name: `WithDataBindMount(IResourceBuilder<KafkaServerResource>, string, bool)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<KafkaServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Kafka/KafkaBuilderExtensions.cs#L198-L203)

Adds a bind mount for the data folder to a Kafka container resource.

```csharp
public static class KafkaBuilderExtensions
{
    public static IResourceBuilder<KafkaServerResource> WithDataBindMount(
        this IResourceBuilder<KafkaServerResource> builder,
        string source,
        bool isReadOnly = false)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<KafkaServerResource>`)
  The resource builder.
- `source` (`string`)
  The source directory on the host to mount into the container.
- `isReadOnly` (`bool`) `optional`
  A flag that indicates if this is a read-only mount.

## Returns

`IResourceBuilder<KafkaServerResource>` -- The `ApplicationModel.IResourceBuilder`1`.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithDataVolume(IResourceBuilder<KafkaServerResource>, string?, bool)

- Name: `WithDataVolume(IResourceBuilder<KafkaServerResource>, string?, bool)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<KafkaServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Kafka/KafkaBuilderExtensions.cs#L180-L184)

Adds a named volume for the data folder to a Kafka container resource.

```csharp
public static class KafkaBuilderExtensions
{
    public static IResourceBuilder<KafkaServerResource> WithDataVolume(
        this IResourceBuilder<KafkaServerResource> builder,
        string? name = null,
        bool isReadOnly = false)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<KafkaServerResource>`)
  The resource builder.
- `name` (`string?`) `optional`
  The name of the volume. Defaults to an auto-generated name based on the application and resource names.
- `isReadOnly` (`bool`) `optional`
  A flag that indicates if this is a read-only volume.

## Returns

`IResourceBuilder<KafkaServerResource>` -- The `ApplicationModel.IResourceBuilder`1`.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithHostPort(IResourceBuilder<KafkaUIContainerResource>, int?)

- Name: `WithHostPort(IResourceBuilder<KafkaUIContainerResource>, int?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<KafkaUIContainerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Kafka/KafkaBuilderExtensions.cs#L161-L166)

Configures the host port that the KafkaUI resource is exposed on instead of using randomly assigned port.

```csharp
public static class KafkaBuilderExtensions
{
    public static IResourceBuilder<KafkaUIContainerResource> WithHostPort(
        this IResourceBuilder<KafkaUIContainerResource> builder,
        int? port)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<KafkaUIContainerResource>`)
  The resource builder for KafkaUI.
- `port` (`int?`)
  The port to bind on the host. If `null` is used random port will be assigned.

## Returns

`IResourceBuilder<KafkaUIContainerResource>` -- The resource builder for KafkaUI.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithKafkaUI(IResourceBuilder<KafkaServerResource>, Action<IResourceBuilder<KafkaUIContainerResource>>, string?)

- Name: `WithKafkaUI(IResourceBuilder<KafkaServerResource>, Action<IResourceBuilder<KafkaUIContainerResource>>, string?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<KafkaServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Kafka/KafkaBuilderExtensions.cs#L97-L135)

Adds a Kafka UI container to the application.

```csharp
public static class KafkaBuilderExtensions
{
    public static IResourceBuilder<KafkaServerResource> WithKafkaUI(
        this IResourceBuilder<KafkaServerResource> builder,
        Action<IResourceBuilder<KafkaUIContainerResource>>? configureContainer = null,
        string? containerName = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<KafkaServerResource>`)
  The Kafka server resource builder.
- `configureContainer` (`Action<IResourceBuilder<KafkaUIContainerResource>>`) `optional`
  Configuration callback for KafkaUI container resource.
- `containerName` (`string?`) `optional`
  The name of the container (Optional).

## Returns

`IResourceBuilder<KafkaServerResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## Remarks

This version of the package defaults to the tag of the container image.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.
