# AzureEventHubsExtensions Methods

- Package: [Aspire.Hosting.Azure.EventHubs](/reference/api/csharp/aspire.hosting.azure.eventhubs.md)
- Type: [AzureEventHubsExtensions](/reference/api/csharp/aspire.hosting.azure.eventhubs/azureeventhubsextensions.md)
- Kind: `Methods`
- Members: `13`

Provides extension methods for adding the Azure Event Hubs resources to the application model.

## AddAzureEventHubs(IDistributedApplicationBuilder, string)

- Name: `AddAzureEventHubs(IDistributedApplicationBuilder, string)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureEventHubsResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.EventHubs/AzureEventHubsExtensions.cs#L47-L131)

Adds an Azure Event Hubs Namespace resource to the application model. This resource can be used to create Event Hub resources.

```csharp
public static class AzureEventHubsExtensions
{
    public static IResourceBuilder<AzureEventHubsResource> AddAzureEventHubs(
        this IDistributedApplicationBuilder builder,
        string name)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IDistributedApplicationBuilder`)
  The builder for the distributed application.
- `name` (`string`)
  The name of the resource.

## Returns

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

## Remarks

By default references to the Azure AppEvent Hubs Namespace resource will be assigned the following roles: - `EventHubsBuiltInRole.AzureEventHubsDataOwner` These can be replaced by calling [AzureEventHubsExtensions.WithRoleAssignments(IResourceBuilder<T>, IResourceBuilder<AzureEventHubsResource>, EventHubsBuiltInRole[])](/reference/api/csharp/aspire.hosting.azure.eventhubs/azureeventhubsextensions/methods.md#withroleassignments-iresourcebuilder-t-iresourcebuilder-azureeventhubsresource-eventhubsbuiltinrole).

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## AddConsumerGroup(IResourceBuilder<AzureEventHubResource>, string, string?)

- Name: `AddConsumerGroup(IResourceBuilder<AzureEventHubResource>, string, string?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureEventHubConsumerGroupResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.EventHubs/AzureEventHubsExtensions.cs#L206-L215)

Adds an Azure Event Hub Consumer Group resource to the application model.

```csharp
public static class AzureEventHubsExtensions
{
    public static IResourceBuilder<AzureEventHubConsumerGroupResource> AddConsumerGroup(
        this IResourceBuilder<AzureEventHubResource> builder,
        string name,
        string? groupName = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AzureEventHubResource>`)
  The Azure Event Hub resource builder.
- `name` (`string`)
  The name of the Event Hub Consumer Group resource.
- `groupName` (`string?`) `optional`
  The name of the Consumer Group. If not provided, this defaults to the same value as `name`.

## Returns

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

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## AddEventHub(IResourceBuilder<AzureEventHubsResource>, string)

> **Obsolete:** This method is obsolete because it has the wrong return type and will be removed in a future version. Use AddHub instead to add an Azure Event Hub.

- Name: `AddEventHub(IResourceBuilder<AzureEventHubsResource>, string)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureEventHubsResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.EventHubs/AzureEventHubsExtensions.cs#L143-L148)

Adds an Azure Event Hubs hub resource to the application model. This resource requires an [AzureEventHubsResource](/reference/api/csharp/aspire.hosting.azure.eventhubs/azureeventhubsresource.md) to be added to the application model.

```csharp
public static class AzureEventHubsExtensions
{
    public static IResourceBuilder<AzureEventHubsResource> AddEventHub(
        this IResourceBuilder<AzureEventHubsResource> builder,
        string name)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AzureEventHubsResource>`)
  The Azure Event Hubs resource builder.
- `name` (`string`)
  The name of the Event Hub.

## Returns

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

## AddHub(IResourceBuilder<AzureEventHubsResource>, string, string?)

- Name: `AddHub(IResourceBuilder<AzureEventHubsResource>, string, string?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureEventHubResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.EventHubs/AzureEventHubsExtensions.cs#L162-L171)

Adds an Azure Event Hubs hub resource to the application model.

```csharp
public static class AzureEventHubsExtensions
{
    public static IResourceBuilder<AzureEventHubResource> AddHub(
        this IResourceBuilder<AzureEventHubsResource> builder,
        string name,
        string? hubName = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AzureEventHubsResource>`)
  The Azure Event Hubs resource builder.
- `name` (`string`)
  The name of the Event Hub resource.
- `hubName` (`string?`) `optional`
  The name of the Event Hub. If not provided, this defaults to the same value as `name`.

## Returns

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

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## RunAsEmulator(IResourceBuilder<AzureEventHubsResource>, Action<IResourceBuilder<AzureEventHubsEmulatorResource>>)

- Name: `RunAsEmulator(IResourceBuilder<AzureEventHubsResource>, Action<IResourceBuilder<AzureEventHubsEmulatorResource>>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureEventHubsResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.EventHubs/AzureEventHubsExtensions.cs#L251-L360)

Configures an Azure Event Hubs resource to be emulated. This resource requires an [AzureEventHubsResource](/reference/api/csharp/aspire.hosting.azure.eventhubs/azureeventhubsresource.md) to be added to the application model.

```csharp
public static class AzureEventHubsExtensions
{
    public static IResourceBuilder<AzureEventHubsResource> RunAsEmulator(
        this IResourceBuilder<AzureEventHubsResource> builder,
        Action<IResourceBuilder<AzureEventHubsEmulatorResource>>? configureContainer = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AzureEventHubsResource>`)
  The Azure Event Hubs resource builder.
- `configureContainer` (`Action<IResourceBuilder<AzureEventHubsEmulatorResource>>`) `optional`
  Callback that exposes underlying container used for emulation to allow for customization.

## Returns

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

## Remarks

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

## Examples

The following example creates an Azure Event Hubs resource that runs locally is an emulator and referencing that resource in a .NET project.

```csharp
var builder = DistributedApplication.CreateBuilder(args);

var eventHub = builder.AddAzureEventHubs("eventhubns")
   .RunAsEmulator()
   .AddEventHub("hub");

builder.AddProject<Projects.InventoryService>()
       .WithReference(eventHub);

builder.Build().Run();
```

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithConfiguration(IResourceBuilder<AzureEventHubsEmulatorResource>, Action<JsonNode>)

- Name: `WithConfiguration(IResourceBuilder<AzureEventHubsEmulatorResource>, Action<JsonNode>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureEventHubsEmulatorResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.EventHubs/AzureEventHubsExtensions.cs#L448-L453)

Alters the JSON configuration document used by the emulator.

```csharp
public static class AzureEventHubsExtensions
{
    public static IResourceBuilder<AzureEventHubsEmulatorResource> WithConfiguration(
        this IResourceBuilder<AzureEventHubsEmulatorResource> builder,
        Action<JsonNode> configJson)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AzureEventHubsEmulatorResource>`)
  The builder for the [AzureEventHubsEmulatorResource](/reference/api/csharp/aspire.hosting.azure.eventhubs/azureeventhubsemulatorresource.md).
- `configJson` (`Action<JsonNode>`)
  A callback to update the JSON object representation of the configuration.

## Returns

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

## Remarks

This method is not available in polyglot app hosts. Use [AzureEventHubsExtensions.WithConfigurationFile(IResourceBuilder<AzureEventHubsEmulatorResource>, string)](/reference/api/csharp/aspire.hosting.azure.eventhubs/azureeventhubsextensions/methods.md#withconfigurationfile-iresourcebuilder-azureeventhubsemulatorresource-string) instead.

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.

## WithConfigurationFile(IResourceBuilder<AzureEventHubsEmulatorResource>, string)

- Name: `WithConfigurationFile(IResourceBuilder<AzureEventHubsEmulatorResource>, string)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureEventHubsEmulatorResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.EventHubs/AzureEventHubsExtensions.cs#L432-L435)

Adds a bind mount for the configuration file of an Azure Event Hubs emulator resource.

```csharp
public static class AzureEventHubsExtensions
{
    public static IResourceBuilder<AzureEventHubsEmulatorResource> WithConfigurationFile(
        this IResourceBuilder<AzureEventHubsEmulatorResource> builder,
        string path)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AzureEventHubsEmulatorResource>`)
  The builder for the [AzureEventHubsEmulatorResource](/reference/api/csharp/aspire.hosting.azure.eventhubs/azureeventhubsemulatorresource.md).
- `path` (`string`)
  Path to the file on the AppHost where the emulator configuration is located.

## Returns

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

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithDataBindMount(IResourceBuilder<AzureEventHubsEmulatorResource>, string?)

> **Obsolete:** This method is obsolete because it doesn't work as intended and will be removed in a future version.

- Name: `WithDataBindMount(IResourceBuilder<AzureEventHubsEmulatorResource>, string?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureEventHubsEmulatorResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.EventHubs/AzureEventHubsExtensions.cs#L372-L374)

Adds a bind mount for the data folder to an Azure Event Hubs emulator resource.

```csharp
public static class AzureEventHubsExtensions
{
    public static IResourceBuilder<AzureEventHubsEmulatorResource> WithDataBindMount(
        this IResourceBuilder<AzureEventHubsEmulatorResource> builder,
        string? path = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AzureEventHubsEmulatorResource>`)
  The builder for the [AzureEventHubsEmulatorResource](/reference/api/csharp/aspire.hosting.azure.eventhubs/azureeventhubsemulatorresource.md).
- `path` (`string?`) `optional`
  Relative path to the AppHost where emulator storage is persisted between runs. Defaults to the path '.eventhubs/{builder.Resource.Name}'

## Returns

`IResourceBuilder<AzureEventHubsEmulatorResource>` -- A builder for the [AzureEventHubsEmulatorResource](/reference/api/csharp/aspire.hosting.azure.eventhubs/azureeventhubsemulatorresource.md).

## WithDataVolume(IResourceBuilder<AzureEventHubsEmulatorResource>, string?)

> **Obsolete:** This method is obsolete because it doesn't work as intended and will be removed in a future version.

- Name: `WithDataVolume(IResourceBuilder<AzureEventHubsEmulatorResource>, string?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureEventHubsEmulatorResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.EventHubs/AzureEventHubsExtensions.cs#L386-L388)

Adds a named volume for the data folder to an Azure Event Hubs emulator resource.

```csharp
public static class AzureEventHubsExtensions
{
    public static IResourceBuilder<AzureEventHubsEmulatorResource> WithDataVolume(
        this IResourceBuilder<AzureEventHubsEmulatorResource> builder,
        string? name = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AzureEventHubsEmulatorResource>`)
  The builder for the [AzureEventHubsEmulatorResource](/reference/api/csharp/aspire.hosting.azure.eventhubs/azureeventhubsemulatorresource.md).
- `name` (`string?`) `optional`
  The name of the volume. Defaults to an auto-generated name based on the application and resource names.

## Returns

`IResourceBuilder<AzureEventHubsEmulatorResource>` -- A builder for the [AzureEventHubsEmulatorResource](/reference/api/csharp/aspire.hosting.azure.eventhubs/azureeventhubsemulatorresource.md).

## WithGatewayPort(IResourceBuilder<AzureEventHubsEmulatorResource>, int?)

> **Obsolete:** Use WithHostPort instead.

- Name: `WithGatewayPort(IResourceBuilder<AzureEventHubsEmulatorResource>, int?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureEventHubsEmulatorResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.EventHubs/AzureEventHubsExtensions.cs#L400-L402)

Configures the host port for the Azure Event Hubs emulator is exposed on instead of using randomly assigned port.

```csharp
public static class AzureEventHubsExtensions
{
    public static IResourceBuilder<AzureEventHubsEmulatorResource> WithGatewayPort(
        this IResourceBuilder<AzureEventHubsEmulatorResource> builder,
        int? port)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AzureEventHubsEmulatorResource>`)
  Builder for the Azure Event Hubs emulator container
- `port` (`int?`)
  The port to bind on the host. If `null` is used, a random port will be assigned.

## Returns

`IResourceBuilder<AzureEventHubsEmulatorResource>` -- Azure Event Hubs emulator resource builder.

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

- Name: `WithHostPort(IResourceBuilder<AzureEventHubsEmulatorResource>, int?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureEventHubsEmulatorResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.EventHubs/AzureEventHubsExtensions.cs#L414-L419)

Configures the host port for the Azure Event Hubs emulator is exposed on instead of using randomly assigned port.

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

## Parameters

- `builder` (`IResourceBuilder<AzureEventHubsEmulatorResource>`)
  Builder for the Azure Event Hubs emulator container
- `port` (`int?`)
  The port to bind on the host. If `null` is used, a random port will be assigned.

## Returns

`IResourceBuilder<AzureEventHubsEmulatorResource>` -- Azure Event Hubs emulator resource builder.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithProperties(IResourceBuilder<AzureEventHubResource>, Action<AzureEventHubResource>)

- Name: `WithProperties(IResourceBuilder<AzureEventHubResource>, Action<AzureEventHubResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureEventHubResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.EventHubs/AzureEventHubsExtensions.cs#L184-L189)

Allows setting the properties of an Azure Event Hub resource.

```csharp
public static class AzureEventHubsExtensions
{
    public static IResourceBuilder<AzureEventHubResource> WithProperties(
        this IResourceBuilder<AzureEventHubResource> builder,
        Action<AzureEventHubResource> configure)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AzureEventHubResource>`)
  The Azure Event Hub resource builder.
- `configure` (`Action<AzureEventHubResource>`)
  A method that can be used for customizing the [AzureEventHubResource](/reference/api/csharp/aspire.hosting.azure.eventhubs/azureeventhubresource.md).

## Returns

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

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithRoleAssignments(IResourceBuilder<T>, IResourceBuilder<AzureEventHubsResource>, EventHubsBuiltInRole[])

- Name: `WithRoleAssignments(IResourceBuilder<T>, IResourceBuilder<AzureEventHubsResource>, EventHubsBuiltInRole[])`
- Modifiers: `extension`
- Returns: `IResourceBuilder<T>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.EventHubs/AzureEventHubsExtensions.cs)

Assigns the specified roles to the given resource, granting it the necessary permissions on the target Azure Event Hubs Namespace resource. This replaces the default role assignments for the resource.

```csharp
public static class AzureEventHubsExtensions
{
    public static IResourceBuilder<T> WithRoleAssignments<T>(
        this IResourceBuilder<T> builder,
        IResourceBuilder<AzureEventHubsResource> target,
        params EventHubsBuiltInRole[] roles)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<T>`)
  The resource to which the specified roles will be assigned.
- `target` (`IResourceBuilder<AzureEventHubsResource>`)
  The target Azure Event Hubs Namespace resource.
- `roles` (`EventHubsBuiltInRole[]`)
  The built-in Event Hubs roles to be assigned.

## Returns

`IResourceBuilder<T>` -- The updated `ApplicationModel.IResourceBuilder`1` with the applied role assignments.

## Remarks

This overload is not available in polyglot app hosts. Use [AzureEventHubsExtensions.WithRoleAssignments(IResourceBuilder<T>, IResourceBuilder<AzureEventHubsResource>, EventHubsBuiltInRole[])](/reference/api/csharp/aspire.hosting.azure.eventhubs/azureeventhubsextensions/methods.md#withroleassignments-iresourcebuilder-t-iresourcebuilder-azureeventhubsresource-eventhubsbuiltinrole) instead.

## Examples

Assigns the AzureEventHubsDataSender role to the 'Projects.Api' project.

```csharp
var builder = DistributedApplication.CreateBuilder(args);

var eventHubs = builder.AddAzureEventHubs("eventHubs");

var api = builder.AddProject<Projects.Api>("api")
  .WithRoleAssignments(eventHubs, EventHubsBuiltInRole.AzureEventHubsDataSender)
  .WithReference(eventHubs);
```

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.
