Get started with the NATS integration
Este conteúdo não está disponível em sua língua ainda.
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.
Set up hosting integration
Section titled “Set up hosting integration”To begin, install the Aspire NATS hosting integration in your Aspire AppHost project:
aspire add natsA Aspire CLI é interativa; escolha o resultado adequado quando solicitado:
Select an integration to add:
> nats (Aspire.Hosting.Nats)> Other results listed as selectable options...#:package Aspire.Hosting.Nats@*<PackageReference Include="Aspire.Hosting.Nats" Version="*" />Next, in the AppHost project, create instances of NATS resources and pass them to the consuming client projects:
var builder = DistributedApplication.CreateBuilder(args);
var nats = builder.AddNats("nats");
builder.AddProject<Projects.ExampleProject>() .WithReference(nats);
builder.Build().Run();Set up client integration
Section titled “Set up client integration”To get started with the Aspire NATS client integration, install the package:
dotnet add package Aspire.NATS.Net#:package Aspire.NATS.Net@*<PackageReference Include="Aspire.NATS.Net" Version="*" />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...}