Gå til indhold

Get started with the NATS integration

Dette indhold er ikke tilgængeligt i dit sprog endnu.

NATS logo

NATS is a simple, secure and high-performance open-source messaging system for cloud-native applications, IoT messaging, and microservices architectures. The Aspire NATS integration provides a way to connect to existing NATS instances or create new instances from Aspire with the docker.io/library/nats container image.

In this introduction, you’ll see how to install and use the Aspire NATS integrations in a simple configuration. If you already have this knowledge, see NATS hosting integration for full reference details.

To begin, install the Aspire NATS hosting integration in your Aspire AppHost project:

Aspire CLI — Tilføj Aspire.Hosting.Nats-pakke
aspire add nats

Aspire CLI er interaktiv; vælg det passende søgeresultat når du bliver spurgt:

Aspire CLI — Eksempel output
Select an integration to add:
> nats (Aspire.Hosting.Nats)
> Other results listed as selectable options...

Next, in the AppHost project, create instances of NATS resources and pass them to the consuming client projects:

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

To get started with the Aspire NATS client integration, install the package:

.NET CLI — Add Aspire.NATS.Net package
dotnet add package Aspire.NATS.Net

In the Program.cs file of your client-consuming project, call the AddNatsClient extension method to register an INatsConnection for use through dependency injection:

builder.AddNatsClient(connectionName: "nats");

You can then retrieve the INatsConnection instance using dependency injection:

public class ExampleService(INatsConnection connection)
{
// Use connection...
}