# AspireMicrosoftAzureCosmosExtensions Methods

- Package: [Aspire.Microsoft.Azure.Cosmos](/reference/api/csharp/aspire.microsoft.azure.cosmos.md)
- Type: [AspireMicrosoftAzureCosmosExtensions](/reference/api/csharp/aspire.microsoft.azure.cosmos/aspiremicrosoftazurecosmosextensions.md)
- Kind: `Methods`
- Members: `6`

Azure Cosmos DB extension

## AddAzureCosmosClient(IHostApplicationBuilder, string, Action<MicrosoftAzureCosmosSettings>, Action<CosmosClientOptions>)

- Name: `AddAzureCosmosClient(IHostApplicationBuilder, string, Action<MicrosoftAzureCosmosSettings>, Action<CosmosClientOptions>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.Microsoft.Azure.Cosmos/AspireMicrosoftAzureCosmosExtensions.cs#L36-L39)

Registers `Cosmos.CosmosClient` as a singleton in the services provided by the `builder`. Configures logging and telemetry for the `Cosmos.CosmosClient`.

```csharp
public static class AspireMicrosoftAzureCosmosExtensions
{
    public static void AddAzureCosmosClient(
        this IHostApplicationBuilder builder,
        string connectionName,
        Action<MicrosoftAzureCosmosSettings>? configureSettings = null,
        Action<CosmosClientOptions>? configureClientOptions = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
  The `Hosting.IHostApplicationBuilder` to read config from and add services to.
- `connectionName` (`string`)
  The connection name to use to find a connection string.
- `configureSettings` (`Action<MicrosoftAzureCosmosSettings>`) `optional`
  An optional method that can be used for customizing the [MicrosoftAzureCosmosSettings](/reference/api/csharp/aspire.microsoft.azure.cosmos/microsoftazurecosmossettings.md). It's invoked after the settings are read from the configuration.
- `configureClientOptions` (`Action<CosmosClientOptions>`) `optional`
  An optional method that can be used for customizing the `Cosmos.CosmosClientOptions`.

## Exceptions

- `InvalidOperationException` -- If required ConnectionString is not provided in configuration section

## Remarks

Reads the configuration from "Aspire:Microsoft:Azure:Cosmos" section.

## AddAzureCosmosContainer(IHostApplicationBuilder, string, Action<MicrosoftAzureCosmosSettings>, Action<CosmosClientOptions>)

- Name: `AddAzureCosmosContainer(IHostApplicationBuilder, string, Action<MicrosoftAzureCosmosSettings>, Action<CosmosClientOptions>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.Microsoft.Azure.Cosmos/AspireMicrosoftAzureCosmosExtensions.cs#L65-L76)

Registers the `Cosmos.Container` as a singleton in the services provided by the `builder`.

```csharp
public static class AspireMicrosoftAzureCosmosExtensions
{
    public static void AddAzureCosmosContainer(
        this IHostApplicationBuilder builder,
        string connectionName,
        Action<MicrosoftAzureCosmosSettings>? configureSettings = null,
        Action<CosmosClientOptions>? configureClientOptions = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
  The `Hosting.IHostApplicationBuilder` to read config from and add services to.
- `connectionName` (`string`)
  The connection name to use to find a connection string.
- `configureSettings` (`Action<MicrosoftAzureCosmosSettings>`) `optional`
  An optional method that can be used for customizing the [MicrosoftAzureCosmosSettings](/reference/api/csharp/aspire.microsoft.azure.cosmos/microsoftazurecosmossettings.md). It's invoked after the settings are read from the configuration.
- `configureClientOptions` (`Action<CosmosClientOptions>`) `optional`
  An optional method that can be used for customizing the `Cosmos.CosmosClientOptions`.

## Exceptions

- `InvalidOperationException` -- If required ConnectionString is not provided in configuration section

## Remarks

Reads the configuration from "Aspire:Microsoft:Azure:Cosmos" section.

## AddAzureCosmosDatabase(IHostApplicationBuilder, string, Action<MicrosoftAzureCosmosSettings>, Action<CosmosClientOptions>)

- Name: `AddAzureCosmosDatabase(IHostApplicationBuilder, string, Action<MicrosoftAzureCosmosSettings>, Action<CosmosClientOptions>)`
- Modifiers: `extension`
- Returns: [CosmosDatabaseBuilder](/reference/api/csharp/aspire.microsoft.azure.cosmos/cosmosdatabasebuilder.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.Microsoft.Azure.Cosmos/AspireMicrosoftAzureCosmosExtensions.cs#L158-L162)

Registers the `Cosmos.Database` as a singleton the services provided by the `builder` and returns a [CosmosDatabaseBuilder](/reference/api/csharp/aspire.microsoft.azure.cosmos/cosmosdatabasebuilder.md) to support chaining multiple container registrations against the same database.

```csharp
public static class AspireMicrosoftAzureCosmosExtensions
{
    public static CosmosDatabaseBuilder AddAzureCosmosDatabase(
        this IHostApplicationBuilder builder,
        string connectionName,
        Action<MicrosoftAzureCosmosSettings>? configureSettings = null,
        Action<CosmosClientOptions>? configureClientOptions = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
  The `Hosting.IHostApplicationBuilder` to read config from and add services to.
- `connectionName` (`string`)
  The connection name to use to find a connection string.
- `configureSettings` (`Action<MicrosoftAzureCosmosSettings>`) `optional`
  An optional method that can be used for customizing the [MicrosoftAzureCosmosSettings](/reference/api/csharp/aspire.microsoft.azure.cosmos/microsoftazurecosmossettings.md). It's invoked after the settings are read from the configuration.
- `configureClientOptions` (`Action<CosmosClientOptions>`) `optional`
  An optional method that can be used for customizing the `Cosmos.CosmosClientOptions`.

## Exceptions

- `InvalidOperationException` -- If required ConnectionString is not provided in configuration section

## Remarks

Reads the configuration from "Aspire:Microsoft:Azure:Cosmos:{name}" section.

## AddKeyedAzureCosmosClient(IHostApplicationBuilder, string, Action<MicrosoftAzureCosmosSettings>, Action<CosmosClientOptions>)

- Name: `AddKeyedAzureCosmosClient(IHostApplicationBuilder, string, Action<MicrosoftAzureCosmosSettings>, Action<CosmosClientOptions>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.Microsoft.Azure.Cosmos/AspireMicrosoftAzureCosmosExtensions.cs#L94-L103)

Registers the `Cosmos.CosmosClient` as a singleton for given `name` in the services provided by the `builder`. Configures logging and telemetry for the `Cosmos.CosmosClient`.

```csharp
public static class AspireMicrosoftAzureCosmosExtensions
{
    public static void AddKeyedAzureCosmosClient(
        this IHostApplicationBuilder builder,
        string name,
        Action<MicrosoftAzureCosmosSettings>? configureSettings = null,
        Action<CosmosClientOptions>? configureClientOptions = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
  The `Hosting.IHostApplicationBuilder` to read config from and add services to.
- `name` (`string`)
  The name of the component, which is used as the `ServiceDescriptor.ServiceKey` of the service and also to retrieve the connection string from the ConnectionStrings configuration section.
- `configureSettings` (`Action<MicrosoftAzureCosmosSettings>`) `optional`
  An optional method that can be used for customizing the [MicrosoftAzureCosmosSettings](/reference/api/csharp/aspire.microsoft.azure.cosmos/microsoftazurecosmossettings.md). It's invoked after the settings are read from the configuration.
- `configureClientOptions` (`Action<CosmosClientOptions>`) `optional`
  An optional method that can be used for customizing the `Cosmos.CosmosClientOptions`.

## Exceptions

- `InvalidOperationException` -- If required ConnectionString is not provided in configuration section

## Remarks

Reads the configuration from "Aspire:Microsoft:Azure:Cosmos:{name}" section.

## AddKeyedAzureCosmosContainer(IHostApplicationBuilder, string, Action<MicrosoftAzureCosmosSettings>, Action<CosmosClientOptions>)

- Name: `AddKeyedAzureCosmosContainer(IHostApplicationBuilder, string, Action<MicrosoftAzureCosmosSettings>, Action<CosmosClientOptions>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.Microsoft.Azure.Cosmos/AspireMicrosoftAzureCosmosExtensions.cs#L129-L140)

Registers the `Cosmos.Container` as a singleton for given `name` in the services provided by the `builder`.

```csharp
public static class AspireMicrosoftAzureCosmosExtensions
{
    public static void AddKeyedAzureCosmosContainer(
        this IHostApplicationBuilder builder,
        string name,
        Action<MicrosoftAzureCosmosSettings>? configureSettings = null,
        Action<CosmosClientOptions>? configureClientOptions = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
  The `Hosting.IHostApplicationBuilder` to read config from and add services to.
- `name` (`string`)
  The name of the component, which is used as the `ServiceDescriptor.ServiceKey` of the service and also to retrieve the connection string from the ConnectionStrings configuration section.
- `configureSettings` (`Action<MicrosoftAzureCosmosSettings>`) `optional`
  An optional method that can be used for customizing the [MicrosoftAzureCosmosSettings](/reference/api/csharp/aspire.microsoft.azure.cosmos/microsoftazurecosmossettings.md). It's invoked after the settings are read from the configuration.
- `configureClientOptions` (`Action<CosmosClientOptions>`) `optional`
  An optional method that can be used for customizing the `Cosmos.CosmosClientOptions`.

## Exceptions

- `InvalidOperationException` -- If required ConnectionString is not provided in configuration section

## Remarks

Reads the configuration from "Aspire:Microsoft:Azure:Cosmos:{name}" section.

## AddKeyedAzureCosmosDatabase(IHostApplicationBuilder, string, Action<MicrosoftAzureCosmosSettings>, Action<CosmosClientOptions>)

- Name: `AddKeyedAzureCosmosDatabase(IHostApplicationBuilder, string, Action<MicrosoftAzureCosmosSettings>, Action<CosmosClientOptions>)`
- Modifiers: `extension`
- Returns: [CosmosDatabaseBuilder](/reference/api/csharp/aspire.microsoft.azure.cosmos/cosmosdatabasebuilder.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.Microsoft.Azure.Cosmos/AspireMicrosoftAzureCosmosExtensions.cs#L181-L185)

Registers the `Cosmos.Database` as a singleton for given `name` in the services provided by the `builder` and returns a [CosmosDatabaseBuilder](/reference/api/csharp/aspire.microsoft.azure.cosmos/cosmosdatabasebuilder.md) to support chaining multiple container registrations against the same database.

```csharp
public static class AspireMicrosoftAzureCosmosExtensions
{
    public static CosmosDatabaseBuilder AddKeyedAzureCosmosDatabase(
        this IHostApplicationBuilder builder,
        string name,
        Action<MicrosoftAzureCosmosSettings>? configureSettings = null,
        Action<CosmosClientOptions>? configureClientOptions = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
  The `Hosting.IHostApplicationBuilder` to read config from and add services to.
- `name` (`string`)
  The name of the component, which is used as the `ServiceDescriptor.ServiceKey` of the service and also to retrieve the connection string from the ConnectionStrings configuration section.
- `configureSettings` (`Action<MicrosoftAzureCosmosSettings>`) `optional`
  An optional method that can be used for customizing the [MicrosoftAzureCosmosSettings](/reference/api/csharp/aspire.microsoft.azure.cosmos/microsoftazurecosmossettings.md). It's invoked after the settings are read from the configuration.
- `configureClientOptions` (`Action<CosmosClientOptions>`) `optional`
  An optional method that can be used for customizing the `Cosmos.CosmosClientOptions`.

## Exceptions

- `InvalidOperationException` -- If required ConnectionString is not provided in configuration section

## Remarks

Reads the configuration from "Aspire:Microsoft:Azure:Cosmos:{name}" section.
