Перейти к содержимому

NATS hosting integration

Это содержимое пока не доступно на вашем языке.

NATS logo

To get started with the Aspire NATS hosting integration, install the Aspire.Hosting.Nats NuGet package in the app host project.

Aspire CLI — Добавить пакет Aspire.Hosting.Nats
aspire add nats

Aspire CLI интерактивен; выберите подходящий результат поиска при запросе:

Aspire CLI — Пример вывода
Select an integration to add:
> nats (Aspire.Hosting.Nats)
> Other results listed as selectable options...

In your app host project, register and consume a NATS server integration using the AddNats extension method to add the NATS server resource to the builder:

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

To add NATS with JetStream enabled, which provides streaming capabilities, use the WithJetStream extension method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var nats = builder.AddNats("nats")
.WithJetStream();
builder.AddProject<Projects.ExampleProject>()
.WithReference(nats);

To add data volumes for persisting NATS data, call the WithDataVolume method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var nats = builder.AddNats("nats")
.WithJetStream()
.WithDataVolume();
builder.AddProject<Projects.ExampleProject>()
.WithReference(nats);

The data volume is used to persist the NATS data outside the lifecycle of the container.

For development scenarios, you may want to use bind mounts instead of data volumes. To add a data bind mount, call the WithDataBindMount method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var nats = builder.AddNats("nats")
.WithJetStream()
.WithDataBindMount(source: @"C:\Nats\Data");
builder.AddProject<Projects.ExampleProject>()
.WithReference(nats);

To add NATS with username and password authentication, use the WithNatsUser and WithNatsPassword parameters:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var user = builder.AddParameter("natsUser");
var pass = builder.AddParameter("natsPass", secret: true);
var nats = builder.AddNats("nats", userName: user, password: pass);
builder.AddProject<Projects.ExampleProject>()
.WithReference(nats);