From 7cccb5367be309f9adb5af84d66b85ab42a64c8b Mon Sep 17 00:00:00 2001 From: David Pine Date: Wed, 5 Jun 2024 19:04:28 -0500 Subject: [PATCH] Add missing code, and add flexible PostgreSQL (#1046) * Ok, I got a little carried away, but these were missing. Also, fixes #428 * Added a few words about execution context * Apply suggestions from code review Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> * Apply suggestions from code review --------- Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- docs/azureai/azureai-openai-component.md | 6 +- .../azureai-search-document-component.md | 4 +- docs/caching/stackexchange-redis-component.md | 6 +- ...nge-redis-distributed-caching-component.md | 6 +- ...exchange-redis-output-caching-component.md | 6 +- docs/database/azure-cosmos-db-component.md | 6 +- ...re-cosmos-db-entity-framework-component.md | 6 +- .../includes/postgresql-flexible-server.md | 34 +++++++++ docs/database/mongodb-component.md | 4 +- docs/database/mysql-component.md | 4 +- .../mysql-entity-framework-component.md | 4 +- .../oracle-entity-framework-component.md | 2 + docs/database/postgresql-component.md | 6 +- .../postgresql-entity-framework-component.md | 6 +- docs/database/qdrant-component.md | 2 + docs/database/sql-server-component.md | 4 +- .../sql-server-entity-framework-component.md | 4 +- docs/fundamentals/app-host-overview.md | 27 +++++++ docs/logging/seq-component.md | 4 +- docs/messaging/azure-event-hubs-component.md | 6 +- docs/messaging/azure-service-bus-component.md | 8 +-- docs/messaging/kafka-component.md | 4 +- docs/messaging/nats-component.md | 72 +++++++++---------- docs/messaging/rabbitmq-client-component.md | 6 +- .../azure-security-key-vault-component.md | 6 +- docs/storage/azure-storage-blobs-component.md | 4 +- .../storage/azure-storage-queues-component.md | 4 +- .../storage/azure-storage-tables-component.md | 6 +- 28 files changed, 176 insertions(+), 81 deletions(-) create mode 100644 docs/database/includes/postgresql-flexible-server.md diff --git a/docs/azureai/azureai-openai-component.md b/docs/azureai/azureai-openai-component.md index ce470e216..124d34598 100644 --- a/docs/azureai/azureai-openai-component.md +++ b/docs/azureai/azureai-openai-component.md @@ -2,7 +2,7 @@ title: .NET Aspire Azure AI OpenAI component description: Learn how to use the .NET Aspire Azure AI OpenAI component. ms.topic: how-to -ms.date: 05/14/2024 +ms.date: 06/05/2024 --- # .NET Aspire Azure AI OpenAI component @@ -74,12 +74,12 @@ dotnet add package Aspire.Hosting.Azure.CognitiveServices In your app host project, register an Azure AI OpenAI resource using the following methods, such as : ```csharp -// Service registration +var builder = DistributedApplication.CreateBuilder(args); + var openai = builder.ExecutionContext.IsPublishMode ? builder.AddAzureOpenAI("openAiConnectionName") : builder.AddConnectionString("openAiConnectionName"); -// Service consumption builder.AddProject() .WithReference(openai); ``` diff --git a/docs/azureai/azureai-search-document-component.md b/docs/azureai/azureai-search-document-component.md index 3033bb32b..cc603d43e 100644 --- a/docs/azureai/azureai-search-document-component.md +++ b/docs/azureai/azureai-search-document-component.md @@ -2,7 +2,7 @@ title: .NET Aspire Azure AI Search Documents component description: Learn how to use the .NET Aspire Azure AI Search Documents component. ms.topic: how-to -ms.date: 05/14/2024 +ms.date: 06/05/2024 --- # .NET Aspire Azure AI Search Documents component @@ -95,6 +95,8 @@ dotnet add package Aspire.Hosting.Azure.CognitiveServices In the _:::no-loc text="Program.cs":::_ file of `AppHost`, add an Azure Search service and consume the connection using the following methods: ```csharp +var builder = DistributedApplication.CreateBuilder(args); + var search = builder.ExecutionContext.IsPublishMode ? builder.AddAzureSearch("search") : builder.AddConnectionString("search"); diff --git a/docs/caching/stackexchange-redis-component.md b/docs/caching/stackexchange-redis-component.md index e4673bd41..3edcf8a20 100644 --- a/docs/caching/stackexchange-redis-component.md +++ b/docs/caching/stackexchange-redis-component.md @@ -2,7 +2,7 @@ title: .NET Aspire StackExchange Redis component description: This article describes the .NET Aspire StackExchange Redis component features and capabilities ms.topic: how-to -ms.date: 05/14/2024 +ms.date: 06/05/2024 --- # .NET Aspire StackExchange Redis component @@ -52,10 +52,10 @@ public class ExampleService(IConnectionMultiplexer connectionMultiplexer) [!INCLUDE [redis-app-host](includes/redis-app-host.md)] ```csharp -// Service registration +var builder = DistributedApplication.CreateBuilder(args); + var redis = builder.AddRedis("redis"); -// Service consumption builder.AddProject() .WithReference(redis) ``` diff --git a/docs/caching/stackexchange-redis-distributed-caching-component.md b/docs/caching/stackexchange-redis-distributed-caching-component.md index 276c9d005..135ea5db1 100644 --- a/docs/caching/stackexchange-redis-distributed-caching-component.md +++ b/docs/caching/stackexchange-redis-distributed-caching-component.md @@ -2,7 +2,7 @@ title: .NET Aspire StackExchange Redis distributed caching component description: This article describes the .NET Aspire StackExchange Redis distributed caching component features and capabilities ms.topic: how-to -ms.date: 05/14/2024 +ms.date: 06/05/2024 --- # .NET Aspire StackExchange Redis distributed caching component @@ -52,10 +52,10 @@ public class ExampleService(IDistributedCache cache) [!INCLUDE [redis-app-host](includes/redis-app-host.md)] ```csharp -// Service registration +var builder = DistributedApplication.CreateBuilder(args); + var redis = builder.AddRedis("redis"); -// Service consumption builder.AddProject() .WithReference(redis) ``` diff --git a/docs/caching/stackexchange-redis-output-caching-component.md b/docs/caching/stackexchange-redis-output-caching-component.md index 0db2825d0..3862ec24f 100644 --- a/docs/caching/stackexchange-redis-output-caching-component.md +++ b/docs/caching/stackexchange-redis-output-caching-component.md @@ -2,7 +2,7 @@ title: .NET Aspire StackExchange Redis output caching Component description: This article describes the .NET Aspire StackExchange Redis output caching component features and capabilities ms.topic: how-to -ms.date: 04/29/2024 +ms.date: 06/05/2024 --- # .NET Aspire StackExchange Redis output caching component @@ -59,10 +59,10 @@ For apps with controllers, apply the `[OutputCache]` attribute to the action met [!INCLUDE [redis-app-host](includes/redis-app-host.md)] ```csharp -// Service registration +var builder = DistributedApplication.CreateBuilder(args); + var redis = builder.AddRedis("redis"); -// Service consumption var basket = builder.AddProject() .WithReference(redis) ``` diff --git a/docs/database/azure-cosmos-db-component.md b/docs/database/azure-cosmos-db-component.md index d9b191c07..2fc95d9e9 100644 --- a/docs/database/azure-cosmos-db-component.md +++ b/docs/database/azure-cosmos-db-component.md @@ -2,7 +2,7 @@ title: .NET Aspire Azure Cosmos DB component description: This article describes the .NET Aspire Azure Cosmos DB component features and capabilities. ms.topic: how-to -ms.date: 05/14/2024 +ms.date: 06/05/2024 --- # .NET Aspire Azure Cosmos DB component @@ -71,11 +71,11 @@ dotnet add package Aspire.Hosting.Azure.CosmosDB In your app host project, register the .NET Aspire Azure Cosmos DB component and consume the service using the following methods: ```csharp -// Service registration +var builder = DistributedApplication.CreateBuilder(args); + var cosmos = builder.AddAzureCosmosDB("cosmos"); var cosmosdb = cosmos.AddDatabase("cosmosdb"); -// Service consumption var exampleProject = builder.AddProject() .WithReference(cosmosdb); ``` diff --git a/docs/database/azure-cosmos-db-entity-framework-component.md b/docs/database/azure-cosmos-db-entity-framework-component.md index 920d1f614..b2a89cb37 100644 --- a/docs/database/azure-cosmos-db-entity-framework-component.md +++ b/docs/database/azure-cosmos-db-entity-framework-component.md @@ -2,7 +2,7 @@ title: .NET Aspire Microsoft Entity Framework Core Cosmos DB component description: This article describes the .NET Aspire Microsoft Entity Framework Core Cosmos DB component features and capabilities. ms.topic: how-to -ms.date: 05/14/2024 +ms.date: 06/05/2024 --- # .NET Aspire Microsoft Entity Framework Core Cosmos DB component @@ -71,11 +71,11 @@ dotnet add package Aspire.Hosting.Azure.CosmosDB In your app host project, register the .NET Aspire Microsoft Entity Framework Core Cosmos DB component and consume the service using the following methods: ```csharp -// Service registration +var builder = DistributedApplication.CreateBuilder(args); + var cosmos = builder.AddAzureCosmosDB("cosmos"); var cosmosdb = cosmos.AddDatabase("cosmosdb"); -// Service consumption var exampleProject = builder.AddProject() .WithReference(cosmosdb); ``` diff --git a/docs/database/includes/postgresql-flexible-server.md b/docs/database/includes/postgresql-flexible-server.md new file mode 100644 index 000000000..19c6530eb --- /dev/null +++ b/docs/database/includes/postgresql-flexible-server.md @@ -0,0 +1,34 @@ +### Azure app host usage + +To deploy your PostgreSQL resources to Azure, you need to install the appropriate .NET Aspire hosting package: + +### [.NET CLI](#tab/dotnet-cli) + +```dotnetcli +dotnet add package Aspire.Hosting.Azure.PostgreSQL +``` + +### [PackageReference](#tab/package-reference) + +```xml + +``` + +--- + +After you've installed this package, you specify that your PostgreSQL resources will be hosted in Azure by calling the extension method in your app host project: + +```csharp +var builder = DistributedApplication.CreateBuilder(args); + +var postgres = builder.AddPostgres("postgres") + .PublishAsAzurePostgresFlexibleServer(); + +var postgresdb = postgres.AddDatabase("postgresdb"); + +var exampleProject = builder.AddProject() + .WithReference(postgresdb); +``` + +The preceding call to `PublishAsAzurePostgresFlexibleServer` configures Postgres Server resource to be deployed as Azure Postgres Flexible Server. For more information, see [Azure Postgres Flexible Server](/azure/postgresql/flexible-server/overview). diff --git a/docs/database/mongodb-component.md b/docs/database/mongodb-component.md index 8e1a6ddc3..b654fd5c6 100644 --- a/docs/database/mongodb-component.md +++ b/docs/database/mongodb-component.md @@ -2,7 +2,7 @@ title: .NET Aspire MongoDB database component description: This article describes the .NET Aspire MongoDB database component. ms.topic: how-to -ms.date: 05/14/2024 +ms.date: 06/05/2024 --- # .NET Aspire MongoDB database component @@ -79,6 +79,8 @@ dotnet add package Aspire.Hosting.MongoDB In your app host project, register the MongoDB database and consume the connection method and consume the service using the following methods: ```csharp +var builder = DistributedApplication.CreateBuilder(args); + var mongo = builder.AddMongoDB("mongo"); var mongodb = mongo.AddDatabase("mongodb"); diff --git a/docs/database/mysql-component.md b/docs/database/mysql-component.md index 19fd1264b..a919f6858 100644 --- a/docs/database/mysql-component.md +++ b/docs/database/mysql-component.md @@ -2,7 +2,7 @@ title: .NET Aspire MySQL database component description: This article describes the .NET Aspire MySQL database component. ms.topic: how-to -ms.date: 06/03/2024 +ms.date: 06/05/2024 --- # .NET Aspire MySQL database component @@ -62,6 +62,8 @@ After adding a `MySqlDataSource`, you can require the `MySqlDataSource` instance [!INCLUDE [mysql-app-host](includes/mysql-app-host.md)] ```csharp +var builder = DistributedApplication.CreateBuilder(args); + var mysql = builder.AddMySql("mysql"); var mysqldb = mysql.AddDatabase("mysqldb"); diff --git a/docs/database/mysql-entity-framework-component.md b/docs/database/mysql-entity-framework-component.md index efefbfbfd..d97b2b88d 100644 --- a/docs/database/mysql-entity-framework-component.md +++ b/docs/database/mysql-entity-framework-component.md @@ -1,7 +1,7 @@ --- title: MySQL Entity Framework Component description: MySQL Entity Framework Component -ms.date: 05/14/2024 +ms.date: 06/05/2024 --- # .NET Aspire Pomelo MySQL Entity Framework Component @@ -61,6 +61,8 @@ builder.EnrichMySqlDbContext(); [!INCLUDE [mysql-app-host](includes/mysql-app-host.md)] ```csharp +var builder = DistributedApplication.CreateBuilder(args); + var mysql = builder.AddMySql("mysql"); var mysqldb = mysql.AddDatabase("mysqldb"); diff --git a/docs/database/oracle-entity-framework-component.md b/docs/database/oracle-entity-framework-component.md index 184789725..a4c449866 100644 --- a/docs/database/oracle-entity-framework-component.md +++ b/docs/database/oracle-entity-framework-component.md @@ -79,6 +79,8 @@ dotnet add package Aspire.Hosting.Oracle In your app host project, register an Oracle container and consume the connection using the following methods: ```csharp +var builder = DistributedApplication.CreateBuilder(args); + var oracle = builder.AddOracle("oracle"); var oracledb = oracle.AddDatabase("oracledb"); diff --git a/docs/database/postgresql-component.md b/docs/database/postgresql-component.md index 7c243da0c..f74653455 100644 --- a/docs/database/postgresql-component.md +++ b/docs/database/postgresql-component.md @@ -1,7 +1,7 @@ --- title: .NET Aspire PostgreSQL component description: This article describes the .NET Aspire PostgreSQL component. -ms.date: 06/03/2024 +ms.date: 06/05/2024 ms.topic: how-to --- @@ -51,6 +51,8 @@ public class ExampleService(NpgsqlDataSource dataSource) [!INCLUDE [postgresql-app-host](includes/postgresql-app-host.md)] ```csharp +var builder = DistributedApplication.CreateBuilder(args); + var postgres = builder.AddPostgres("postgres"); var postgresdb = postgres.AddDatabase("postgresdb"); @@ -60,6 +62,8 @@ var exampleProject = builder.AddProject() [!INCLUDE [postgresql-explicit-username-password](includes/postgresql-explicit-username-password.md)] +[!INCLUDE [postgresql-flexible-server](includes/postgresql-flexible-server.md)] + ## Configuration The .NET Aspire PostgreSQL component provides multiple configuration approaches and options to meet the requirements and conventions of your project. diff --git a/docs/database/postgresql-entity-framework-component.md b/docs/database/postgresql-entity-framework-component.md index a273f1e4f..e8f389c3d 100644 --- a/docs/database/postgresql-entity-framework-component.md +++ b/docs/database/postgresql-entity-framework-component.md @@ -2,7 +2,7 @@ title: .NET Aspire PostgreSQL Entity Framework Core component description: This article describes the .NET Aspire PostgreSQL Entity Framework Core component. ms.topic: how-to -ms.date: 06/03/2024 +ms.date: 06/05/2024 --- # .NET Aspire PostgreSQL Entity Framework Core component @@ -65,6 +65,8 @@ public class ExampleService(YourDbContext context) [!INCLUDE [postgresql-app-host](includes/postgresql-app-host.md)] ```csharp +var builder = DistributedApplication.CreateBuilder(args); + var postgres = builder.AddPostgres("postgres"); var postgresdb = postgres.AddDatabase("postgresdb"); @@ -74,6 +76,8 @@ var myService = builder.AddProject() [!INCLUDE [postgresql-explicit-username-password](includes/postgresql-explicit-username-password.md)] +[!INCLUDE [postgresql-flexible-server](includes/postgresql-flexible-server.md)] + ## Configuration The .NET Aspire PostgreSQL Entity Framework Core component provides multiple configuration approaches and options to meet the requirements and conventions of your project. diff --git a/docs/database/qdrant-component.md b/docs/database/qdrant-component.md index 93e6171b0..bc32c640b 100644 --- a/docs/database/qdrant-component.md +++ b/docs/database/qdrant-component.md @@ -69,6 +69,8 @@ dotnet add package Aspire.Hosting.Qdrant In your app host project, register a Qdrant server and consume the connection using the following methods: ```csharp +var builder = DistributedApplication.CreateBuilder(args); + var qdrant = builder.AddQdrant("qdrant"); var myService = builder.AddProject() diff --git a/docs/database/sql-server-component.md b/docs/database/sql-server-component.md index b32cc4255..20c02b7ec 100644 --- a/docs/database/sql-server-component.md +++ b/docs/database/sql-server-component.md @@ -2,7 +2,7 @@ title: .NET Aspire SQL Server component description: This article describes the .NET Aspire SQL Server component. ms.topic: how-to -ms.date: 06/03/2024 +ms.date: 06/05/2024 --- # .NET Aspire SQL Server component @@ -64,6 +64,8 @@ After adding a `SqlConnection`, you can get the scoped [SqlConnection](/dotnet/a [!INCLUDE [sql-app-host](includes/sql-app-host.md)] ```csharp +var builder = DistributedApplication.CreateBuilder(args); + var sql = builder.AddSqlServer("sql"); var sqldb = sql.AddDatabase("sqldb"); diff --git a/docs/database/sql-server-entity-framework-component.md b/docs/database/sql-server-entity-framework-component.md index 3661e8d15..1bee77328 100644 --- a/docs/database/sql-server-entity-framework-component.md +++ b/docs/database/sql-server-entity-framework-component.md @@ -2,7 +2,7 @@ title: .NET Aspire SqlServer Entity Framework Core component description: This article describes the .NET Aspire SQL Server Entity Framework Core component. ms.topic: how-to -ms.date: 05/14/2024 +ms.date: 06/05/2024 --- # .NET Aspire SqlServer Entity Framework Core component @@ -61,6 +61,8 @@ public class ExampleService(YourDbContext client) [!INCLUDE [sql-app-host](includes/sql-app-host.md)] ```csharp +var builder = DistributedApplication.CreateBuilder(args); + var sql = builder.AddSqlServer("sql"); var sqldb = sql.AddDatabase("sqldb"); diff --git a/docs/fundamentals/app-host-overview.md b/docs/fundamentals/app-host-overview.md index af277636f..c5adb3199 100644 --- a/docs/fundamentals/app-host-overview.md +++ b/docs/fundamentals/app-host-overview.md @@ -256,6 +256,33 @@ Beyond the base resource types, exposes an execution context (), which provides information about the current execution of the app host. This context can be used to evaluate whether or not the app host is executing as "run" mode, or as part of a publish operation. Consider the following: + +- : Returns `true` if the current operation is running. +- : Returns `true` if the current operation is publishing. + +This information can be useful when you want to conditionally execute code based on the current operation. Consider the following example that demonstrates using the `IsRunMode` property. In this case, an extension method is used to generate a stable node name for RabbitMQ for local development runs. + +```csharp +private static IResourceBuilder RunWithStableNodeName( + this IResourceBuilder builder) +{ + if (builder.ApplicationBuilder.ExecutionContext.IsRunMode) + { + builder.WithEnvironment(context => + { + // Set a stable node name so queue storage is consistent between sessions + var nodeName = $"{builder.Resource.Name}@localhost"; + context.EnvironmentVariables["RABBITMQ_NODENAME"] = nodeName; + }); + } + + return builder; +} +``` + ## See also - [.NET Aspire components overview](components-overview.md) diff --git a/docs/logging/seq-component.md b/docs/logging/seq-component.md index ffefa4410..cb550b889 100644 --- a/docs/logging/seq-component.md +++ b/docs/logging/seq-component.md @@ -2,7 +2,7 @@ title: .NET Aspire Seq component description: Learn how to use the .NET Aspire Seq component to add OpenTelemetry Protocol (OTLP) exporters that send logs and traces to a Seq Server. ms.topic: how-to -ms.date: 05/29/2024 +ms.date: 06/05/2024 --- # .NET Aspire Seq component @@ -62,6 +62,8 @@ dotnet add package Aspire.Hosting.Seq In your app host project, register a Seq database and consume the connection using the following methods: ```csharp +var builder = DistributedApplication.CreateBuilder(args); + var seq = builder.AddSeq("seq") .ExcludeFromManifest(); diff --git a/docs/messaging/azure-event-hubs-component.md b/docs/messaging/azure-event-hubs-component.md index f490113cf..42b881c95 100644 --- a/docs/messaging/azure-event-hubs-component.md +++ b/docs/messaging/azure-event-hubs-component.md @@ -2,7 +2,7 @@ title: .NET Aspire Azure Event Hubs component description: This article describes the .NET Aspire Azure Event Hubs component features and capabilities. ms.topic: how-to -ms.date: 05/14/2024 +ms.date: 06/05/2024 --- # .NET Aspire Azure Event Hubs component @@ -89,8 +89,10 @@ dotnet add package Aspire.Hosting.Azure.EventHubs In your app host project, add an Event Hubs connection and an Event Hub resource and consume the connection using the following methods: ```csharp +var builder = DistributedApplication.CreateBuilder(args); + var eventHubs = builder.AddAzureEventHubs("eventHubsConnectionName") - .AddEventHub("MyHub");; + .AddEventHub("MyHub"); var ExampleService = builder.AddProject() .WithReference(eventHubs); diff --git a/docs/messaging/azure-service-bus-component.md b/docs/messaging/azure-service-bus-component.md index c833fe510..8fb97d6f0 100644 --- a/docs/messaging/azure-service-bus-component.md +++ b/docs/messaging/azure-service-bus-component.md @@ -2,7 +2,7 @@ title: .NET Aspire Azure Service Bus component description: This article describes the .NET Aspire Azure Service Bus component features and capabilities ms.topic: how-to -ms.date: 05/30/2024 +ms.date: 06/05/2024 --- # .NET Aspire Azure Service Bus component @@ -77,12 +77,12 @@ dotnet add package Aspire.Hosting.Azure.ServiceBus In your app host project, register the Service Bus component and consume the service using the following methods: ```csharp -// Service registration +var builder = DistributedApplication.CreateBuilder(args); + var serviceBus = builder.ExecutionContext.IsPublishMode ? builder.AddAzureServiceBus("messaging") : builder.AddConnectionString("messaging"); -// Service consumption builder.AddProject() .WithReference(serviceBus) ``` @@ -194,7 +194,7 @@ The .NET Aspire Azure Service Bus component uses the following log categories: You can enable tracing in several ways: -- Setting the `Azure.Experimental.EnableActivitySource` [runtime configuration setting](https://learn.microsoft.com/dotnet/core/runtime-config/) to `true`. Which can be done with either: +- Setting the `Azure.Experimental.EnableActivitySource` [runtime configuration setting](/dotnet/core/runtime-config/) to `true`. Which can be done with either: - Call `AppContext.SetSwitch("Azure.Experimental.EnableActivitySource", true);`. - Add the `RuntimeHostConfigurationOption` setting to your project file: diff --git a/docs/messaging/kafka-component.md b/docs/messaging/kafka-component.md index a384e2456..4b54e86f7 100644 --- a/docs/messaging/kafka-component.md +++ b/docs/messaging/kafka-component.md @@ -2,7 +2,7 @@ title: .NET Aspire Apache Kafka component description: Learn how to use the .NET Aspire Apache Kafka client message-broker component. ms.topic: how-to -ms.date: 05/14/2024 +ms.date: 06/05/2024 --- # .NET Aspire Apache Kafka component @@ -69,6 +69,8 @@ dotnet add package Aspire.Hosting.Kafka In your app host project, register a Kafka container and consume the connection using the following methods: ```csharp +var builder = DistributedApplication.CreateBuilder(args); + var messaging = builder.AddKafka("messaging"); var myService = builder.AddProject() diff --git a/docs/messaging/nats-component.md b/docs/messaging/nats-component.md index ac3d4ed14..5bc76b2d8 100644 --- a/docs/messaging/nats-component.md +++ b/docs/messaging/nats-component.md @@ -37,8 +37,6 @@ For more information, see [dotnet add package](/dotnet/core/tools/dotnet-add-pac ## Example usage - - In the _:::no-loc text="Program.cs":::_ file of your projects, call the `AddNatsClient` extension method to register an `INatsConnection` to send logs and traces to NATS and the .NET Aspire Dashboard. The method takes a connection name parameter. ```csharp @@ -54,6 +52,42 @@ public class ExampleService(INatsConnection client) } ``` +## App host usage + +To model the Nats resource in the app host, install the [Aspire.Hosting.Nats](https://www.nuget.org/packages/Aspire.Hosting.Nats) NuGet package. + +### [.NET CLI](#tab/dotnet-cli) + +```dotnetcli +dotnet add package Aspire.Hosting.Nats +``` + +### [PackageReference](#tab/package-reference) + +```xml + +``` + +--- + +Then, in the _:::no-loc text="Program.cs":::_ file of `AppHost`, register a NATS server and consume the connection using the following methods: + +```csharp +var builder = DistributedApplication.CreateBuilder(args); + +var nats = builder.AddNats("nats"); + +var myService = builder.AddProject() + .WithReference(nats); +``` + +The `WithReference` method configures a connection in the `MyService` project named `nats`. In the _:::no-loc text="Program.cs":::_ file of `MyService`, the NATS connection can be consumed using: + +```csharp +builder.AddNatsClient("nats"); +``` + ## Configuration The .NET Aspire NATS component provides multiple options to configure the NATS connection based on the requirements and conventions of your project. @@ -102,40 +136,6 @@ Pass the `Action configureSettings` delegate to set up some builder.AddNatsClient("nats", settings => settings.DisableHealthChecks = true); ``` -## App host usage - -To model the Nats resource in the app host, install the [Aspire.Hosting.Nats](https://www.nuget.org/packages/Aspire.Hosting.Nats) NuGet package. - -### [.NET CLI](#tab/dotnet-cli) - -```dotnetcli -dotnet add package Aspire.Hosting.Nats -``` - -### [PackageReference](#tab/package-reference) - -```xml - -``` - ---- - -Then, in the _:::no-loc text="Program.cs":::_ file of `AppHost`, register a NATS server and consume the connection using the following methods: - -```csharp -var nats = builder.AddNats("nats"); - -var myService = builder.AddProject() - .WithReference(nats); -``` - -The `WithReference` method configures a connection in the `MyService` project named `nats`. In the _:::no-loc text="Program.cs":::_ file of `MyService`, the NATS connection can be consumed using: - -```csharp -builder.AddNatsClient("nats"); -``` - ### Persistent logs and traces Register NATS with a data directory in your **.AppHost** project to retain NATS's data and configuration across application restarts. diff --git a/docs/messaging/rabbitmq-client-component.md b/docs/messaging/rabbitmq-client-component.md index 43711073f..538e7a677 100644 --- a/docs/messaging/rabbitmq-client-component.md +++ b/docs/messaging/rabbitmq-client-component.md @@ -2,7 +2,7 @@ title: .NET Aspire RabbitMQ component description: Learn how to use the .NET Aspire RabbitMQ client message-broker component. ms.topic: how-to -ms.date: 06/03/2024 +ms.date: 06/05/2024 --- # .NET Aspire RabbitMQ component @@ -69,10 +69,10 @@ dotnet add package Aspire.Hosting.RabbitMQ In your app host project, register a RabbitMQ server and consume the connection using the following methods, such as : ```csharp -// Service registration +var builder = DistributedApplication.CreateBuilder(args); + var messaging = builder.AddRabbitMQ("messaging"); -// Service consumption builder.AddProject() .WithReference(messaging); ``` diff --git a/docs/security/azure-security-key-vault-component.md b/docs/security/azure-security-key-vault-component.md index 1dd6f0430..d30140c10 100644 --- a/docs/security/azure-security-key-vault-component.md +++ b/docs/security/azure-security-key-vault-component.md @@ -2,7 +2,7 @@ title: .NET Aspire Azure Key Vault component description: Lean about the .NET Aspire Azure Key Vault component. ms.topic: how-to -ms.date: 05/14/2024 +ms.date: 06/05/2024 --- # .NET Aspire Azure Key Vault component @@ -89,12 +89,12 @@ dotnet add package Aspire.Hosting.Azure.KeyVault In your app host project, register the Azure Key Vault component and consume the service using the following methods: ```csharp -// Service registration +var builder = DistributedApplication.CreateBuilder(args); + var secrets = builder.ExecutionContext.IsPublishMode ? builder.AddAzureKeyVault("secrets") : builder.AddConnectionString("secrets"); -// Service consumption builder.AddProject() .WithReference(secrets) ``` diff --git a/docs/storage/azure-storage-blobs-component.md b/docs/storage/azure-storage-blobs-component.md index d08a102a0..0a21bd94e 100644 --- a/docs/storage/azure-storage-blobs-component.md +++ b/docs/storage/azure-storage-blobs-component.md @@ -2,7 +2,7 @@ title: .NET Aspire Azure Blob Storage component description: This article describes the .NET Aspire Azure Blob Storage component features and capabilities. ms.topic: how-to -ms.date: 05/14/2024 +ms.date: 06/05/2024 --- # .NET Aspire Azure Blob Storage component @@ -69,6 +69,8 @@ dotnet add package Aspire.Hosting.Azure.Storage In your app host project, register the Azure Blob Storage component and consume the service using the following methods, such as : ```csharp +var builder = DistributedApplication.CreateBuilder(args); + var blobs = builder.AddAzureStorage("storage") .AddBlobs("blobs"); diff --git a/docs/storage/azure-storage-queues-component.md b/docs/storage/azure-storage-queues-component.md index d3a4e4ec1..c027f1a11 100644 --- a/docs/storage/azure-storage-queues-component.md +++ b/docs/storage/azure-storage-queues-component.md @@ -2,7 +2,7 @@ title: .NET Aspire Azure Queue Storage component description: This article describes the .NET Aspire Azure Queue Storage component features and capabilities ms.topic: how-to -ms.date: 05/14/2024 +ms.date: 06/05/2024 --- # .NET Aspire Azure Queue Storage component @@ -69,6 +69,8 @@ dotnet add package Aspire.Hosting.Azure.Storage In your app host project, add a Storage Queue connection and consume the connection using the following methods, such as : ```csharp +var builder = DistributedApplication.CreateBuilder(args); + var queues = builder.AddAzureStorage("storage") .AddQueues("queues"); diff --git a/docs/storage/azure-storage-tables-component.md b/docs/storage/azure-storage-tables-component.md index 5235ad04c..0b7da7e73 100644 --- a/docs/storage/azure-storage-tables-component.md +++ b/docs/storage/azure-storage-tables-component.md @@ -1,7 +1,7 @@ --- title: .NET Aspire Azure Data Tables component description: This article describes the .NET Aspire Azure Data Tables component features and capabilities. -ms.date: 05/14/2024 +ms.date: 06/05/2024 ms.topic: how-to --- @@ -77,11 +77,11 @@ dotnet add package Aspire.Hosting.Azure.Storage In your app host project, register the Azure Table Storage component and consume the service using the following methods: ```csharp -// Service registration +var builder = DistributedApplication.CreateBuilder(args); + var tables = builder.AddAzureStorage("storage") .AddTables("tables"); -// Service consumption Builder.AddProject() .WithReference(tables) ```