Skip to content

Commit a1f0544

Browse files
alistairmatthewsCopilotIEvangelist
authored
Add section about using InfrastructureResolver classes to the Customize Azure resources article (#4229)
* Added section about using InfrastructureResolver classes to customize Azure resources. * Removed a temporary file. * Fixed broken code links * Correct the ResolveResources method name to ResolveProperties. Co-authored-by: Copilot <[email protected]> * Update docs/azure/customize-azure-resources.md --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: David Pine <[email protected]>
1 parent 8129acd commit a1f0544

File tree

7 files changed

+132
-0
lines changed

7 files changed

+132
-0
lines changed

docs/azure/customize-azure-resources.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ Consider the resulting Bicep file:
8989

9090
The Bicep file reflects the desired configuration of the Azure Container Registry, as defined by the `AddAzureInfrastructure` API.
9191

92+
### Use an infrastructure resolver to customize Azure provisioning options
93+
94+
Another method you can use to customize Azure provisioning is to create an <xref:Azure.Provisioning.Primitives.InfrastructureResolver> and write code in it to implement your requirements. Then add that class to the configuration options for your AppHost.
95+
96+
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.
97+
98+
:::code language="csharp" source="snippets/customize-azure-with-infrastructure-resolver/AppHost.cs" id="infrastructureresolver":::
99+
100+
Having created that class, add it to the configuration options using code like this in the AppHost:
101+
102+
:::code language="csharp" source="snippets/customize-azure-with-infrastructure-resolver/AppHost.cs" id="configureazureoptions":::
103+
92104
## Use custom Bicep templates
93105

94106
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.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Aspire.Hosting.Azure;
2+
using Azure.Provisioning;
3+
using Azure.Provisioning.Primitives;
4+
using Azure.Provisioning.CosmosDB;
5+
using Microsoft.Extensions.DependencyInjection;
6+
7+
// <configureazureoptions>
8+
var builder = DistributedApplication.CreateBuilder(args);
9+
10+
builder.Services.Configure<AzureProvisioningOptions>(options =>
11+
{
12+
options.ProvisioningBuildOptions.InfrastructureResolvers.Insert(0, new FixedNameInfrastructureResolver());
13+
});
14+
15+
builder.Build().Run();
16+
// </configureazureoptions>
17+
18+
// <infrastructureresolver>
19+
internal sealed class FixedNameInfrastructureResolver : InfrastructureResolver
20+
{
21+
public override void ResolveProperties(ProvisionableConstruct construct, ProvisioningBuildOptions options)
22+
{
23+
if (construct is CosmosDBAccount account)
24+
{
25+
account.Name = "ContosoCosmosDb";
26+
}
27+
28+
base.ResolveProperties(construct, options);
29+
}
30+
}
31+
// </infrastructureresolver>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<Sdk Name="Aspire.AppHost.Sdk" Version="9.4.0" />
4+
5+
<PropertyGroup>
6+
<OutputType>Exe</OutputType>
7+
<TargetFramework>net9.0</TargetFramework>
8+
<ImplicitUsings>enable</ImplicitUsings>
9+
<Nullable>enable</Nullable>
10+
<UserSecretsId>0f6d6204-f9d8-4ca7-bae5-dccb1b132bbf</UserSecretsId>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.4.0" />
15+
<PackageReference Include="Azure.Provisioning" Version="1.3.0" />
16+
<PackageReference Include="Aspire.Hosting.Azure.CosmosDB" Version="9.4.0" />
17+
</ItemGroup>
18+
19+
</Project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"https": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": true,
8+
"applicationUrl": "https://localhost:17032;http://localhost:15110",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development",
11+
"DOTNET_ENVIRONMENT": "Development",
12+
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21110",
13+
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22097"
14+
}
15+
},
16+
"http": {
17+
"commandName": "Project",
18+
"dotnetRunMessages": true,
19+
"launchBrowser": true,
20+
"applicationUrl": "http://localhost:15110",
21+
"environmentVariables": {
22+
"ASPNETCORE_ENVIRONMENT": "Development",
23+
"DOTNET_ENVIRONMENT": "Development",
24+
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19243",
25+
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20044"
26+
}
27+
}
28+
}
29+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning",
6+
"Aspire.Hosting.Dcp": "Warning"
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.5.2.0
4+
MinimumVisualStudioVersion = 10.0.40219.1
5+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CustomizeAzureWithInfrastructureResolver", "CustomizeAzureWithInfrastructureResolver.csproj", "{F6E1845D-4EE9-780B-E79F-F87BD599761D}"
6+
EndProject
7+
Global
8+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9+
Debug|Any CPU = Debug|Any CPU
10+
Release|Any CPU = Release|Any CPU
11+
EndGlobalSection
12+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
13+
{F6E1845D-4EE9-780B-E79F-F87BD599761D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
14+
{F6E1845D-4EE9-780B-E79F-F87BD599761D}.Debug|Any CPU.Build.0 = Debug|Any CPU
15+
{F6E1845D-4EE9-780B-E79F-F87BD599761D}.Release|Any CPU.ActiveCfg = Release|Any CPU
16+
{F6E1845D-4EE9-780B-E79F-F87BD599761D}.Release|Any CPU.Build.0 = Release|Any CPU
17+
EndGlobalSection
18+
GlobalSection(SolutionProperties) = preSolution
19+
HideSolutionNode = FALSE
20+
EndGlobalSection
21+
GlobalSection(ExtensibilityGlobals) = postSolution
22+
SolutionGuid = {83D94A4D-6BB3-4B50-A1F7-C86F42B676DE}
23+
EndGlobalSection
24+
EndGlobal

0 commit comments

Comments
 (0)