# AspireNatsClientExtensions Methods

- Package: [Aspire.NATS.Net](/reference/api/csharp/aspire.nats.net.md)
- Type: [AspireNatsClientExtensions](/reference/api/csharp/aspire.nats.net/aspirenatsclientextensions.md)
- Kind: `Methods`
- Members: `13`

Extension methods for connecting NATS server with NATS client

## AddKeyedNatsClient(IHostApplicationBuilder, string)

- Name: `AddKeyedNatsClient(IHostApplicationBuilder, string)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.NATS.Net/AspireNatsClientExtensions.cs#L63-L66)

```csharp
public static class AspireNatsClientExtensions
{
    public static void AddKeyedNatsClient(
        this IHostApplicationBuilder builder,
        string name)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
- `name` (`string`)

## AddKeyedNatsClient(IHostApplicationBuilder, string, Action<NatsClientSettings>)

- Name: `AddKeyedNatsClient(IHostApplicationBuilder, string, Action<NatsClientSettings>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.NATS.Net/AspireNatsClientExtensions.cs#L71-L74)

```csharp
public static class AspireNatsClientExtensions
{
    public static void AddKeyedNatsClient(
        this IHostApplicationBuilder builder,
        string name,
        Action<NatsClientSettings>? configureSettings)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
- `name` (`string`)
- `configureSettings` (`Action<NatsClientSettings>`)

## AddKeyedNatsClient(IHostApplicationBuilder, string, Func<NatsOpts, NatsOpts>)

- Name: `AddKeyedNatsClient(IHostApplicationBuilder, string, Func<NatsOpts, NatsOpts>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.NATS.Net/AspireNatsClientExtensions.cs)

```csharp
public static class AspireNatsClientExtensions
{
    public static void AddKeyedNatsClient(
        this IHostApplicationBuilder builder,
        string name,
        Func<NatsOpts, NatsOpts>? configureOptions)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
- `name` (`string`)
- `configureOptions` (`Func<NatsOpts, NatsOpts>`)

## AddKeyedNatsClient(IHostApplicationBuilder, string, Func<IServiceProvider, NatsOpts, NatsOpts>)

- Name: `AddKeyedNatsClient(IHostApplicationBuilder, string, Func<IServiceProvider, NatsOpts, NatsOpts>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.NATS.Net/AspireNatsClientExtensions.cs)

```csharp
public static class AspireNatsClientExtensions
{
    public static void AddKeyedNatsClient(
        this IHostApplicationBuilder builder,
        string name,
        Func<IServiceProvider, NatsOpts, NatsOpts>? configureOptions)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
- `name` (`string`)
- `configureOptions` (`Func<IServiceProvider, NatsOpts, NatsOpts>`)

## AddKeyedNatsClient(IHostApplicationBuilder, string, Action<NatsClientSettings>, Func<NatsOpts, NatsOpts>)

- Name: `AddKeyedNatsClient(IHostApplicationBuilder, string, Action<NatsClientSettings>, Func<NatsOpts, NatsOpts>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.NATS.Net/AspireNatsClientExtensions.cs)

```csharp
public static class AspireNatsClientExtensions
{
    public static void AddKeyedNatsClient(
        this IHostApplicationBuilder builder,
        string name,
        Action<NatsClientSettings>? configureSettings,
        Func<NatsOpts, NatsOpts>? configureOptions)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
- `name` (`string`)
- `configureSettings` (`Action<NatsClientSettings>`)
- `configureOptions` (`Func<NatsOpts, NatsOpts>`)

## AddKeyedNatsClient(IHostApplicationBuilder, string, Action<NatsClientSettings>, Func<IServiceProvider, NatsOpts, NatsOpts>)

- Name: `AddKeyedNatsClient(IHostApplicationBuilder, string, Action<NatsClientSettings>, Func<IServiceProvider, NatsOpts, NatsOpts>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.NATS.Net/AspireNatsClientExtensions.cs)

Registers `Core.INatsConnection` as a keyed service for given `name` for connecting NATS server with NATS client. Configures health check and logging for the NATS client.

```csharp
public static class AspireNatsClientExtensions
{
    public static void AddKeyedNatsClient(
        this IHostApplicationBuilder builder,
        string name,
        Action<NatsClientSettings>? configureSettings,
        Func<IServiceProvider, NatsOpts, NatsOpts>? configureOptions)
    {
        // ...
    }
}
```

## 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<NatsClientSettings>`)
  An optional delegate that can be used for customizing options. It's invoked after the settings are read from the configuration.
- `configureOptions` (`Func<IServiceProvider, NatsOpts, NatsOpts>`)
  An optional delegate that can be used for customizing NATS options that aren't exposed as standard configuration.

## Exceptions

- `ArgumentNullException` -- Thrown when `builder` or `name` is null.
- `ArgumentException` -- Thrown if mandatory `name` is empty.
- `InvalidOperationException` -- Thrown when mandatory [NatsClientSettings.ConnectionString](/reference/api/csharp/aspire.nats.net/natsclientsettings/properties.md#connectionstring) is not provided.

## AddNatsClient(IHostApplicationBuilder, string)

- Name: `AddNatsClient(IHostApplicationBuilder, string)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.NATS.Net/AspireNatsClientExtensions.cs#L27)

```csharp
public static class AspireNatsClientExtensions
{
    public static void AddNatsClient(
        this IHostApplicationBuilder builder,
        string connectionName)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
- `connectionName` (`string`)

## AddNatsClient(IHostApplicationBuilder, string, Action<NatsClientSettings>)

- Name: `AddNatsClient(IHostApplicationBuilder, string, Action<NatsClientSettings>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.NATS.Net/AspireNatsClientExtensions.cs#L31)

```csharp
public static class AspireNatsClientExtensions
{
    public static void AddNatsClient(
        this IHostApplicationBuilder builder,
        string connectionName,
        Action<NatsClientSettings>? configureSettings)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
- `connectionName` (`string`)
- `configureSettings` (`Action<NatsClientSettings>`)

## AddNatsClient(IHostApplicationBuilder, string, Func<NatsOpts, NatsOpts>)

- Name: `AddNatsClient(IHostApplicationBuilder, string, Func<NatsOpts, NatsOpts>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.NATS.Net/AspireNatsClientExtensions.cs)

```csharp
public static class AspireNatsClientExtensions
{
    public static void AddNatsClient(
        this IHostApplicationBuilder builder,
        string connectionName,
        Func<NatsOpts, NatsOpts>? configureOptions)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
- `connectionName` (`string`)
- `configureOptions` (`Func<NatsOpts, NatsOpts>`)

## AddNatsClient(IHostApplicationBuilder, string, Func<IServiceProvider, NatsOpts, NatsOpts>)

- Name: `AddNatsClient(IHostApplicationBuilder, string, Func<IServiceProvider, NatsOpts, NatsOpts>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.NATS.Net/AspireNatsClientExtensions.cs)

```csharp
public static class AspireNatsClientExtensions
{
    public static void AddNatsClient(
        this IHostApplicationBuilder builder,
        string connectionName,
        Func<IServiceProvider, NatsOpts, NatsOpts>? configureOptions)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
- `connectionName` (`string`)
- `configureOptions` (`Func<IServiceProvider, NatsOpts, NatsOpts>`)

## AddNatsClient(IHostApplicationBuilder, string, Action<NatsClientSettings>, Func<NatsOpts, NatsOpts>)

- Name: `AddNatsClient(IHostApplicationBuilder, string, Action<NatsClientSettings>, Func<NatsOpts, NatsOpts>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.NATS.Net/AspireNatsClientExtensions.cs)

```csharp
public static class AspireNatsClientExtensions
{
    public static void AddNatsClient(
        this IHostApplicationBuilder builder,
        string connectionName,
        Action<NatsClientSettings>? configureSettings,
        Func<NatsOpts, NatsOpts>? configureOptions)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
- `connectionName` (`string`)
- `configureSettings` (`Action<NatsClientSettings>`)
- `configureOptions` (`Func<NatsOpts, NatsOpts>`)

## AddNatsClient(IHostApplicationBuilder, string, Action<NatsClientSettings>, Func<IServiceProvider, NatsOpts, NatsOpts>)

- Name: `AddNatsClient(IHostApplicationBuilder, string, Action<NatsClientSettings>, Func<IServiceProvider, NatsOpts, NatsOpts>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.NATS.Net/AspireNatsClientExtensions.cs)

Registers `Core.INatsConnection` service for connecting NATS server with NATS client. Configures health check and logging for the NATS client.

```csharp
public static class AspireNatsClientExtensions
{
    public static void AddNatsClient(
        this IHostApplicationBuilder builder,
        string connectionName,
        Action<NatsClientSettings>? configureSettings,
        Func<IServiceProvider, NatsOpts, NatsOpts>? configureOptions)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
  The `Hosting.IHostApplicationBuilder` to read config from and add services to.
- `connectionName` (`string`)
  A name used to retrieve the connection string from the ConnectionStrings configuration section.
- `configureSettings` (`Action<NatsClientSettings>`)
  An optional delegate that can be used for customizing options. It's invoked after the settings are read from the configuration.
- `configureOptions` (`Func<IServiceProvider, NatsOpts, NatsOpts>`)
  An optional delegate that can be used for customizing NATS options that aren't exposed as standard configuration.

## Exceptions

- `ArgumentNullException` -- Thrown if mandatory `builder` is null.
- `InvalidOperationException` -- Thrown when mandatory [NatsClientSettings.ConnectionString](/reference/api/csharp/aspire.nats.net/natsclientsettings/properties.md#connectionstring) is not provided.

## AddNatsJetStream(IHostApplicationBuilder)

- Name: `AddNatsJetStream(IHostApplicationBuilder)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.NATS.Net/AspireNatsClientExtensions.cs#L202-L208)

Registers `JetStream.INatsJSContext` service for NATS JetStream operations.

```csharp
public static class AspireNatsClientExtensions
{
    public static void AddNatsJetStream(
        this IHostApplicationBuilder builder)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
  The `Hosting.IHostApplicationBuilder` to read config from and add services to.

## Exceptions

- `ArgumentNullException` -- Thrown if mandatory `builder` is null.
