コンテンツにスキップ
Docs Try Aspire
Docs Try

Set up NATS in the AppHost

このコンテンツはまだ日本語訳がありません。

NATS logo

This article is the reference for the Aspire NATS Hosting integration. It enumerates the AppHost APIs — with examples for both AppHost.cs and apphost.ts — that you use to model a NATS resource in your AppHost project.

If you’re new to the NATS integration, start with the Get started with NATS integrations guide. For how consuming apps read the connection information this page exposes, see Connect to NATS.

To start building an Aspire app that uses NATS, install the 📦 Aspire.Hosting.Nats NuGet package:

Terminal
aspire add nats

Learn more about aspire add in the command reference.

Or, choose a manual installation approach:

C# — AppHost.cs
#:package Aspire.Hosting.Nats@*
XML — AppHost.csproj
<PackageReference Include="Aspire.Hosting.Nats" Version="*" />

Once you’ve installed the hosting integration in your AppHost project, you can add a NATS resource as shown in the following examples:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var nats = builder.AddNats("nats");
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(nats);
// After adding all resources, run the app...
  1. When Aspire adds a container image to the AppHost, as shown in the preceding example with the docker.io/library/nats image, it creates a new NATS instance on your local machine.

  2. The NATS resource is configured with a randomly generated password by default. To set explicit credentials, see Add NATS resource with parameters.

  3. The AppHost reference call configures a connection in the consuming project named after the referenced NATS resource, such as nats in the preceding example.

JetStream is NATS’s built-in persistence engine that provides at-least-once delivery, replay, and stream retention. To enable JetStream on the NATS resource, use WithJetStream (or withJetStream):

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var nats = builder.AddNats("nats")
.WithJetStream();
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(nats);
// After adding all resources, run the app...

Add a data volume to the NATS resource to persist messages and JetStream state across container restarts:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var nats = builder.AddNats("nats")
.WithJetStream()
.WithDataVolume(isReadOnly: false);
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(nats);
// After adding all resources, run the app...

The data volume is used to persist the NATS data outside the lifecycle of its container. The data volume is mounted at the /data path in the NATS container and when a name parameter isn’t provided, the name is generated at random. For more information on data volumes and details on why they’re preferred over bind mounts, see Docker docs: Volumes.

Add a data bind mount to the NATS resource as shown in the following examples:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var nats = builder.AddNats("nats")
.WithJetStream()
.WithDataBindMount(
source: @"C:\Nats\Data",
isReadOnly: false);
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(nats);
// After adding all resources, run the app...

Data bind mounts rely on the host machine’s filesystem to persist NATS data across container restarts. The data bind mount is mounted at the C:\Nats\Data on Windows (or /Nats/Data on Unix) path on the host machine in the NATS container. For more information on data bind mounts, see Docker docs: Bind mounts.

When you want to explicitly provide the port and credentials used by the NATS container, you can pass them as parameters:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var userName = builder.AddParameter("natsUser");
var password = builder.AddParameter("natsPass", secret: true);
var nats = builder.AddNats("nats", userName: userName, password: password);
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(nats);
// After adding all resources, run the app...

When no userName or password parameters are provided, Aspire generates strong credentials automatically using the CreateDefaultPasswordParameter method.

By default, Aspire injects the NATS connection information using variable names derived from the resource name (for example, NATS_URI, NATS_HOST, NATS_PORT). If your consuming app expects a different set of environment variable names, pass individual connection properties from the AppHost:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var nats = builder.AddNats("nats");
var app = builder.AddExecutable("my-app", "node", "app.js", ".")
.WithReference(nats)
.WithEnvironment(context =>
{
context.EnvironmentVariables["NATS_HOST"] = nats.Resource.PrimaryEndpoint.Property(EndpointProperty.Host);
context.EnvironmentVariables["NATS_PORT"] = nats.Resource.PrimaryEndpoint.Property(EndpointProperty.Port);
context.EnvironmentVariables["NATS_USER"] = nats.Resource.UserNameParameter;
context.EnvironmentVariables["NATS_PASSWORD"] = nats.Resource.PasswordParameter;
});
builder.Build().Run();

For the full reference of NATS connection properties — and how consuming apps in C#, TypeScript, Python, and Go read them — see Connect to NATS.

The NATS hosting integration automatically adds a health check for the NATS resource. The health check verifies that the NATS instance is running and that a connection can be established to it.

The hosting integration relies on the 📦 AspNetCore.HealthChecks.Nats NuGet package.