Aspire.NATS.Net 13.4.4

Aspire.NATS.Net library

Registers INatsClient and INatsConnection in the DI container for connecting to a NATS server. Enables corresponding health check, logging and telemetry.

Getting started

Prerequisites

  • NATS server and the server URL for accessing the server.

Install the package

Install the Aspire NATS library with NuGet:

dotnet add package Aspire.NATS.Net

Usage example

In the Program.cs file of your project, call the AddNatsClient extension method to register an INatsClient (and INatsConnection) for use via the dependency injection container. The method takes a connection name parameter.

builder.AddNatsClient("nats");

You can then retrieve an INatsClient instance using dependency injection. For example, to retrieve the client from a Web API controller:

private readonly INatsClient _client;

public ProductsController(INatsClient client)
{
    _client = client;
}

By default, the registered client uses NatsClientDefaultSerializerRegistry which serializes typed payloads as JSON (with raw and UTF-8 primitive support). To use a custom serializer, such as a source-generated NatsJsonContextSerializerRegistry for AOT, pass a configureOptions delegate:

builder.AddNatsClient("nats", configureOptions: opts =>
    opts with { SerializerRegistry = new NatsJsonContextSerializerRegistry(MyJsonContext.Default) });

Configuration

The Aspire NATS component provides multiple options to configure the NATS connection based on the requirements and conventions of your project.

Use a connection string

When using a connection string from the ConnectionStrings configuration section, you can provide the name of the connection string when calling builder.AddNatsClient():

builder.AddNatsClient("myConnection");

And then the connection string will be retrieved from the ConnectionStrings configuration section:

{
  "ConnectionStrings": {
    "myConnection": "nats://nats:4222"
  }
}

See the ConnectionString documentation for more information on how to format this connection string.

Use configuration providers

The Aspire NATS component supports Microsoft.Extensions.Configuration. It loads the NatsClientSettings from configuration by using the Aspire:Nats:Client key. Example appsettings.json that configures some of the options:

{
  "Aspire": {
    "Nats": {
      "Client": {
        "DisableHealthChecks": true
      }
    }
  }
}

Use inline delegates

Also you can pass the Action<NatsClientSettings> configureSettings delegate to set up some or all the options inline, for example to disable health checks from code:

builder.AddNatsClient("nats", settings => settings.DisableHealthChecks = true);

AppHost extensions

In your AppHost project, install the Aspire.Hosting.Nats library with NuGet:

dotnet add package Aspire.Hosting.Nats

Then, in the AppHost.cs file of AppHost, register a NATS server and consume the connection using the following methods:

var nats = builder.AddNats("nats");

var myService = builder.AddProject<Projects.MyService>()
                       .WithReference(nats);

The WithReference method configures a connection in the MyService project named nats. In the Program.cs file of MyService, the NATS connection can be consumed using:

builder.AddNatsClient("nats");

Additional documentation

Feedback & contributing

https://github.com/microsoft/aspire

Showing the top 20 packages that depend on Aspire.NATS.Net.

Packages Downloads
FormCMS
A headless CMS with GraphQL API and drag-and-drop page designer.
42
FormCMS
A headless CMS with GraphQL API and drag-and-drop page designer.
39
FormCMS
A headless CMS with GraphQL API and drag-and-drop page designer.
38
FormCMS
A headless CMS with GraphQL API and drag-and-drop page designer.
37
FormCMS
A headless CMS with GraphQL API and drag-and-drop page designer.
36
FormCMS
A headless CMS with GraphQL API and drag-and-drop page designer.
35
FormCMS
A headless CMS with GraphQL API and drag-and-drop page designer.
34
FormCMS
A headless CMS with GraphQL API and drag-and-drop page designer.
33
FormCMS
A headless CMS with GraphQL API and drag-and-drop page designer.
32
FormCMS
A headless CMS with GraphQL API and drag-and-drop page designer.
31

.NET 8.0

.NET 9.0

.NET 10.0

Version Downloads Last updated
13.4.6 1 06/22/2026
13.4.5 4 06/18/2026
13.4.4 2 06/17/2026
13.4.3 3 06/10/2026
13.4.2 3 06/06/2026
13.4.1 3 06/06/2026
13.4.0 5 06/02/2026
13.3.5 7 05/23/2026
13.3.4 6 05/22/2026
13.3.3 9 05/16/2026
13.3.2 7 05/16/2026
13.3.1 11 05/15/2026
13.3.0 12 05/08/2026
13.2.4 14 04/27/2026
13.2.3 15 04/22/2026
13.2.2 14 04/13/2026
13.2.1 20 04/02/2026
13.2.0 19 03/24/2026
13.1.3 20 03/20/2026
13.1.2 25 02/27/2026
13.1.1 25 02/17/2026
13.1.0 33 12/20/2025
13.0.2 32 12/10/2025
13.0.1 29 12/10/2025
13.0.0 31 12/12/2025
9.5.2 27 12/10/2025
9.5.1 27 12/10/2025
9.5.0 30 12/11/2025
9.4.2 29 12/11/2025
9.4.1 25 12/10/2025
9.4.0 29 12/11/2025
9.3.1 23 12/11/2025
9.3.0 28 12/10/2025
9.2.1 26 12/10/2025
9.2.0 27 12/11/2025
9.1.0 23 12/12/2025
9.0.0 23 12/11/2025
9.0.0-rc.1.24511.1 31 12/11/2025
8.2.2 28 12/10/2025
8.2.1 28 12/11/2025
8.2.0 43 12/10/2025
8.1.0 29 12/11/2025
8.0.2 26 12/11/2025
8.0.1 33 12/10/2025
8.0.0 26 12/11/2025
8.0.0-preview.7.24251.11 27 12/10/2025
8.0.0-preview.6.24214.1 27 12/11/2025
8.0.0-preview.5.24201.12 29 12/10/2025