Replies: 2 comments
-
cc @eerhardt |
Beta Was this translation helpful? Give feedback.
0 replies
-
.NET Aspire 9 added a better way to enable this. There are 2 ways to do this:
builder.AddAzureCosmosDB("ecoDriverCosmosDb")
.ConfigureInfrastructure(infrastructure =>
{
// set the resource's Name to a fixed value
var cosmosAccount = infrastructure.GetProvisionableResources().OfType<CosmosDBAccount>().Single();
cosmosAccount.Name = "ecoDriverCosmosDb";
})
var builder = DistributedApplication.CreateBuilder(args);
builder.Services.Configure<AzureProvisioningOptions>(options =>
{
options.ProvisioningBuildOptions.InfrastructureResolvers.Insert(0, new FixedNameInfrastructureResolver());
});
builder.AddAzureCosmosDB("ecoDriverCosmosDb")
// ...
internal sealed class FixedNameInfrastructureResolver : InfrastructureResolver
{
public override void ResolveProperties(ProvisionableConstruct construct, ProvisioningBuildOptions options)
{
if (construct is CosmosDBAccount account) // whatever check you want here
{
account.Name = "ecoDriverCosmosDb";
}
base.ResolveProperties(construct, options);
}
} NOTE: if you are using 9.0.0-rc.1, the above code won't work. We changed some names in between rc1 and 9.0 GA. To do this in 9.0-rc1, use the following code: builder.AddAzureCosmosDB("ecoDriverCosmosDb")
.ConfigureConstruct(infrastructure =>
{
// set the resource's Name to a fixed value
var cosmosAccount = infrastructure.GetResources().OfType<CosmosDBAccount>().Single();
cosmosAccount.Name = "ecoDriverCosmosDb";
}) and builder.Services.Configure<AzureResourceOptions>(options =>
{
options.ProvisioningContext.PropertyResolvers.Insert(0, new FixedNameInfrastructureResolver());
});
builder.AddAzureCosmosDB("ecoDriverCosmosDb")
// ...
internal sealed class FixedNameInfrastructureResolver : PropertyResolver
{
public override void ResolveProperties(ProvisioningContext context, ProvisioningConstruct construct)
{
if (construct is CosmosDBAccount account) // whatever check you want here
{
account.Name = "ecoDriverCosmosDb";
}
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Today if you do something like:
You'll get a unique name generated in the bicep:
name: toLower(take('ecoDriverCosmosDb${uniqueString(resourceGroup().id)}', 24))
It makes sense that it does the safe thing by default, but is there any way to override this in Aspire (without having to drop down into the Bicep) to a fixed name (no uniqueString)?
It would be quite handy for provisioning a fixed instance within the scope of a resource group. Locally it could provision or use the existing resource with that name, like a shared development database. It would be handy in multi-solution scenarios, not having to copy a connection string round, it can just go off and find that fixed resource.
Also it would look prettier in Azure, and I could make things a bit more readable for me.
Beta Was this translation helpful? Give feedback.
All reactions