From b0f92c883f51a20321b8c1f2b74766052fb9d6fa Mon Sep 17 00:00:00 2001 From: AJ Matthews Date: Wed, 6 Aug 2025 17:00:26 +0100 Subject: [PATCH 1/5] Added section about using InfrastructureResolver classes to customize Azure resources. --- docs/azure/customize-azure-resources.md | 12 +++++++ .../AppHost.cs | 31 +++++++++++++++++++ .../AppHost.cs~ | 27 ++++++++++++++++ ...mizeAzureWithInfrastructureResolver.csproj | 19 ++++++++++++ .../Properties/launchSettings.json | 29 +++++++++++++++++ .../appsettings.Development.json | 8 +++++ .../appsettings.json | 9 ++++++ ...ize-azure-with-infrastructure-resolver.sln | 24 ++++++++++++++ 8 files changed, 159 insertions(+) create mode 100644 docs/azure/snippets/customize-azure-with-infrastructure-resolver/AppHost.cs create mode 100644 docs/azure/snippets/customize-azure-with-infrastructure-resolver/AppHost.cs~ create mode 100644 docs/azure/snippets/customize-azure-with-infrastructure-resolver/CustomizeAzureWithInfrastructureResolver.csproj create mode 100644 docs/azure/snippets/customize-azure-with-infrastructure-resolver/Properties/launchSettings.json create mode 100644 docs/azure/snippets/customize-azure-with-infrastructure-resolver/appsettings.Development.json create mode 100644 docs/azure/snippets/customize-azure-with-infrastructure-resolver/appsettings.json create mode 100644 docs/azure/snippets/customize-azure-with-infrastructure-resolver/customize-azure-with-infrastructure-resolver.sln diff --git a/docs/azure/customize-azure-resources.md b/docs/azure/customize-azure-resources.md index 0db64b27a2..f84456accb 100644 --- a/docs/azure/customize-azure-resources.md +++ b/docs/azure/customize-azure-resources.md @@ -89,6 +89,18 @@ Consider the resulting Bicep file: The Bicep file reflects the desired configuration of the Azure Container Registry, as defined by the `AddAzureInfrastructure` API. +### Use an infrastructure resolver to customize Azure provisioning options + +Another method you can use to customize Azure provisioning is to create an and write code in it to implement your requirements. Then add that class to the configuration options for your AppHost. + +The custom infrastructure resolver is a class that inherits from `InfrastructureResolver` and overrides the `ResolveResources` method to make the customizations you need. In this example, the name of a Cosmos DB resource is set: + +:::code language="csharp" source="snippets/customize-azure-with-infrastructure-resolver/AppHost.cs" id="infrastructure-resolver"::: + +Having created that class, add it to the configuration options using code like this in the AppHost: + +:::code language="csharp" source="snippets/customize-azure-with-infrastructure-resolver/AppHost.cs" id="configure-azure-options"::: + ## Use custom Bicep templates When you're targeting Azure as your desired cloud provider, you can use Bicep to define your infrastructure as code. It aims to drastically simplify the authoring experience with a cleaner syntax and better support for modularity and code reuse. diff --git a/docs/azure/snippets/customize-azure-with-infrastructure-resolver/AppHost.cs b/docs/azure/snippets/customize-azure-with-infrastructure-resolver/AppHost.cs new file mode 100644 index 0000000000..b7865b523e --- /dev/null +++ b/docs/azure/snippets/customize-azure-with-infrastructure-resolver/AppHost.cs @@ -0,0 +1,31 @@ +using Aspire.Hosting.Azure; +using Azure.Provisioning; +using Azure.Provisioning.Primitives; +using Azure.Provisioning.CosmosDB; +using Microsoft.Extensions.DependencyInjection; + +// +var builder = DistributedApplication.CreateBuilder(args); + +builder.Services.Configure(options => +{ + options.ProvisioningBuildOptions.InfrastructureResolvers.Insert(0, new FixedNameInfrastructureResolver()); +}); + +builder.Build().Run(); +// + +// +internal sealed class FixedNameInfrastructureResolver : InfrastructureResolver +{ + public override void ResolveProperties(ProvisionableConstruct construct, ProvisioningBuildOptions options) + { + if (construct is CosmosDBAccount account) + { + account.Name = "ContosoCosmosDb"; + } + + base.ResolveProperties(construct, options); + } +} +// diff --git a/docs/azure/snippets/customize-azure-with-infrastructure-resolver/AppHost.cs~ b/docs/azure/snippets/customize-azure-with-infrastructure-resolver/AppHost.cs~ new file mode 100644 index 0000000000..b1fb20366c --- /dev/null +++ b/docs/azure/snippets/customize-azure-with-infrastructure-resolver/AppHost.cs~ @@ -0,0 +1,27 @@ +using Aspire.Hosting.Azure; +using Azure.Provisioning; +using Azure.Provisioning.Primitives; +using Azure.Provisioning.CosmosDB; +using Microsoft.Extensions.DependencyInjection; + +var builder = DistributedApplication.CreateBuilder(args); + +builder.Services.Configure(options => +{ + options.ProvisioningBuildOptions.InfrastructureResolvers.Insert(0, new FixedNameInfrastructureResolver()); +}); + +builder.Build().Run(); + +internal sealed class FixedNameInfrastructureResolver : InfrastructureResolver +{ + public override void ResolveProperties(ProvisionableConstruct construct, ProvisioningBuildOptions options) + { + if (construct is CosmosDBAccount account) + { + account.Name = "ContosoCosmosDb"; + } + + base.ResolveProperties(construct, options); + } +} \ No newline at end of file diff --git a/docs/azure/snippets/customize-azure-with-infrastructure-resolver/CustomizeAzureWithInfrastructureResolver.csproj b/docs/azure/snippets/customize-azure-with-infrastructure-resolver/CustomizeAzureWithInfrastructureResolver.csproj new file mode 100644 index 0000000000..9e29dde307 --- /dev/null +++ b/docs/azure/snippets/customize-azure-with-infrastructure-resolver/CustomizeAzureWithInfrastructureResolver.csproj @@ -0,0 +1,19 @@ + + + + + + Exe + net9.0 + enable + enable + 0f6d6204-f9d8-4ca7-bae5-dccb1b132bbf + + + + + + + + + diff --git a/docs/azure/snippets/customize-azure-with-infrastructure-resolver/Properties/launchSettings.json b/docs/azure/snippets/customize-azure-with-infrastructure-resolver/Properties/launchSettings.json new file mode 100644 index 0000000000..f8e6093ddd --- /dev/null +++ b/docs/azure/snippets/customize-azure-with-infrastructure-resolver/Properties/launchSettings.json @@ -0,0 +1,29 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:17032;http://localhost:15110", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21110", + "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22097" + } + }, + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:15110", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19243", + "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20044" + } + } + } +} diff --git a/docs/azure/snippets/customize-azure-with-infrastructure-resolver/appsettings.Development.json b/docs/azure/snippets/customize-azure-with-infrastructure-resolver/appsettings.Development.json new file mode 100644 index 0000000000..0c208ae918 --- /dev/null +++ b/docs/azure/snippets/customize-azure-with-infrastructure-resolver/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/docs/azure/snippets/customize-azure-with-infrastructure-resolver/appsettings.json b/docs/azure/snippets/customize-azure-with-infrastructure-resolver/appsettings.json new file mode 100644 index 0000000000..31c092aa45 --- /dev/null +++ b/docs/azure/snippets/customize-azure-with-infrastructure-resolver/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Aspire.Hosting.Dcp": "Warning" + } + } +} diff --git a/docs/azure/snippets/customize-azure-with-infrastructure-resolver/customize-azure-with-infrastructure-resolver.sln b/docs/azure/snippets/customize-azure-with-infrastructure-resolver/customize-azure-with-infrastructure-resolver.sln new file mode 100644 index 0000000000..d940a8c7c0 --- /dev/null +++ b/docs/azure/snippets/customize-azure-with-infrastructure-resolver/customize-azure-with-infrastructure-resolver.sln @@ -0,0 +1,24 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.2.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CustomizeAzureWithInfrastructureResolver", "CustomizeAzureWithInfrastructureResolver.csproj", "{F6E1845D-4EE9-780B-E79F-F87BD599761D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F6E1845D-4EE9-780B-E79F-F87BD599761D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F6E1845D-4EE9-780B-E79F-F87BD599761D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F6E1845D-4EE9-780B-E79F-F87BD599761D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F6E1845D-4EE9-780B-E79F-F87BD599761D}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {83D94A4D-6BB3-4B50-A1F7-C86F42B676DE} + EndGlobalSection +EndGlobal From 65ebce2dadb6d976fe03430df6a58016ce10898e Mon Sep 17 00:00:00 2001 From: AJ Matthews Date: Wed, 6 Aug 2025 17:06:10 +0100 Subject: [PATCH 2/5] Removed a temporary file. --- .../AppHost.cs~ | 27 ------------------- 1 file changed, 27 deletions(-) delete mode 100644 docs/azure/snippets/customize-azure-with-infrastructure-resolver/AppHost.cs~ diff --git a/docs/azure/snippets/customize-azure-with-infrastructure-resolver/AppHost.cs~ b/docs/azure/snippets/customize-azure-with-infrastructure-resolver/AppHost.cs~ deleted file mode 100644 index b1fb20366c..0000000000 --- a/docs/azure/snippets/customize-azure-with-infrastructure-resolver/AppHost.cs~ +++ /dev/null @@ -1,27 +0,0 @@ -using Aspire.Hosting.Azure; -using Azure.Provisioning; -using Azure.Provisioning.Primitives; -using Azure.Provisioning.CosmosDB; -using Microsoft.Extensions.DependencyInjection; - -var builder = DistributedApplication.CreateBuilder(args); - -builder.Services.Configure(options => -{ - options.ProvisioningBuildOptions.InfrastructureResolvers.Insert(0, new FixedNameInfrastructureResolver()); -}); - -builder.Build().Run(); - -internal sealed class FixedNameInfrastructureResolver : InfrastructureResolver -{ - public override void ResolveProperties(ProvisionableConstruct construct, ProvisioningBuildOptions options) - { - if (construct is CosmosDBAccount account) - { - account.Name = "ContosoCosmosDb"; - } - - base.ResolveProperties(construct, options); - } -} \ No newline at end of file From 4cd72f47e7b01ebc5476eddb9a3155f5e6bc6ae5 Mon Sep 17 00:00:00 2001 From: AJ Matthews Date: Wed, 6 Aug 2025 17:12:12 +0100 Subject: [PATCH 3/5] Fixed broken code links --- docs/azure/customize-azure-resources.md | 4 ++-- .../AppHost.cs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/azure/customize-azure-resources.md b/docs/azure/customize-azure-resources.md index f84456accb..62e89a26ed 100644 --- a/docs/azure/customize-azure-resources.md +++ b/docs/azure/customize-azure-resources.md @@ -95,11 +95,11 @@ Another method you can use to customize Azure provisioning is to create an +// var builder = DistributedApplication.CreateBuilder(args); builder.Services.Configure(options => @@ -13,9 +13,9 @@ }); builder.Build().Run(); -// +// -// +// internal sealed class FixedNameInfrastructureResolver : InfrastructureResolver { public override void ResolveProperties(ProvisionableConstruct construct, ProvisioningBuildOptions options) @@ -28,4 +28,4 @@ public override void ResolveProperties(ProvisionableConstruct construct, Provisi base.ResolveProperties(construct, options); } } -// +// From 79c606aa40f2a137ab2e964c4a62035e8c934e30 Mon Sep 17 00:00:00 2001 From: Alistair Matthews Date: Thu, 7 Aug 2025 17:38:52 +0100 Subject: [PATCH 4/5] Correct the ResolveResources method name to ResolveProperties. Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- docs/azure/customize-azure-resources.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/azure/customize-azure-resources.md b/docs/azure/customize-azure-resources.md index 62e89a26ed..a35f9dbf2e 100644 --- a/docs/azure/customize-azure-resources.md +++ b/docs/azure/customize-azure-resources.md @@ -93,7 +93,7 @@ The Bicep file reflects the desired configuration of the Azure Container Registr Another method you can use to customize Azure provisioning is to create an and write code in it to implement your requirements. Then add that class to the configuration options for your AppHost. -The custom infrastructure resolver is a class that inherits from `InfrastructureResolver` and overrides the `ResolveResources` method to make the customizations you need. In this example, the name of a Cosmos DB resource is set: +The custom infrastructure resolver is a class that inherits from `InfrastructureResolver` and overrides the `ResolveProperties` method to make the customizations you need. In this example, the name of a Cosmos DB resource is set: :::code language="csharp" source="snippets/customize-azure-with-infrastructure-resolver/AppHost.cs" id="infrastructureresolver"::: From 9949f651a1de92a035c2bd006b0e80b506553454 Mon Sep 17 00:00:00 2001 From: David Pine Date: Fri, 8 Aug 2025 08:08:10 -0500 Subject: [PATCH 5/5] Update docs/azure/customize-azure-resources.md --- docs/azure/customize-azure-resources.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/azure/customize-azure-resources.md b/docs/azure/customize-azure-resources.md index a35f9dbf2e..479de3aa48 100644 --- a/docs/azure/customize-azure-resources.md +++ b/docs/azure/customize-azure-resources.md @@ -93,7 +93,7 @@ The Bicep file reflects the desired configuration of the Azure Container Registr Another method you can use to customize Azure provisioning is to create an and write code in it to implement your requirements. Then add that class to the configuration options for your AppHost. -The custom infrastructure resolver is a class that inherits from `InfrastructureResolver` and overrides the `ResolveProperties` method to make the customizations you need. In this example, the name of a Cosmos DB resource is set: +A custom infrastructure resolver is a class that inherits from `InfrastructureResolver` and can override one or more of its virtual members to apply the desired customizations. In this example, the `ResolveProperties` method is overridden to set the name of a Cosmos DB resource, but other members can also be overridden depending on your needs. :::code language="csharp" source="snippets/customize-azure-with-infrastructure-resolver/AppHost.cs" id="infrastructureresolver":::