콘텐츠로 이동

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);