microsoft/aspire · error · ArgumentException

Cannot configure custom domain when resource is not…

Error message

Cannot configure custom domain when resource is not parented by ResourceModuleConstruct.

What it means

WithCustomDomain-related configuration (ConfigureCustomDomain) emits Bicep parameters via the resource's parent AzureResourceInfrastructure. If the container app was not created inside a ResourceModuleConstruct-based infrastructure, there is no module to attach the certificate/custom domain parameters to, so an ArgumentException is thrown.

Solutions

  1. Ensure the container app is published through a ResourceModuleConstruct (standard Azure publish flow) before configuring a custom domain
  2. Move the custom domain configuration into the proper publish-time customization callback
  3. Verify you are not calling the API at run time; custom domains only apply to published Azure resources

Example fix

// before
var app = builder.AddProject<Projects.Api>("api");
ConfigureCustomDomain(app, domain, certName); // called outside module construct
// after
builder.AddProject<Projects.Api>("api")
    .PublishAsAzureContainerApp(env, app => ConfigureCustomDomain(app, domain, certName));
Defensive patterns

Strategy: validation

Validate before calling

if (app.ParentInfrastructure is AzureResourceInfrastructure module)
{
    // safe to call ConfigureCustomDomain
}

Type guard

bool canConfigureCustomDomain = app.ParentInfrastructure is AzureResourceInfrastructure;

Try / catch

try { ConfigureCustomDomain(app, domain, certName); }
catch (ArgumentException ex) when (ex.Message.Contains("ResourceModuleConstruct")) { /* app is not in a publishable module */ }

Prevention

When it happens

Trigger: Calling the custom domain extension on a container app whose ParentInfrastructure is not an AzureResourceInfrastructure (e.g. customization callback running against a construct that isn't a ResourceModuleConstruct, or calling outside a publish customization).

Common situations: Using ConfigureCustomDomain in Run mode or in a context where the app isn't part of a publishable module; wrapping the container app in a custom infrastructure type that isn't ResourceModuleConstruct.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/e8d197863b35e64d. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting.Azure.AppContainers/ContainerAppExtensions.cs:66

    /// builder.AddProject&lt;Projects.InventoryService&gt;("inventory")
    ///        .PublishAsAzureContainerApp((module, app) =>
    ///        {
    ///          app.ConfigureCustomDomain(customDomain, certificateName);
    ///        });
    /// </code>
    /// </example>
    /// </remarks>
    /// <ats-remarks />
    [AspireExport]
    public static void ConfigureCustomDomain(this ContainerApp app, IResourceBuilder<ParameterResource> customDomain, IResourceBuilder<ParameterResource> certificateName)
    {
        ArgumentNullException.ThrowIfNull(app);
        ArgumentNullException.ThrowIfNull(customDomain);
        ArgumentNullException.ThrowIfNull(certificateName);

        if (app.ParentInfrastructure is not AzureResourceInfrastructure module)
        {
            throw new ArgumentException("Cannot configure custom domain when resource is not parented by ResourceModuleConstruct.", nameof(app));
        }

        var containerAppManagedEnvironmentId = app.EnvironmentId;
        var certificateNameParameter = certificateName.AsProvisioningParameter(module);
        var customDomainParameter = customDomain.AsProvisioningParameter(module);

        var bindingTypeConditional = new ConditionalExpression(
            new BinaryExpression(
                new IdentifierExpression(certificateNameParameter.BicepIdentifier),
                BinaryBicepOperator.NotEqual,
                new StringLiteralExpression(string.Empty)),
            new StringLiteralExpression("SniEnabled"),
            new StringLiteralExpression("Disabled")
            );

        var certificateOrEmpty = new ConditionalExpression(
            new BinaryExpression(
                new IdentifierExpression(certificateNameParameter.BicepIdentifier),

View on GitHub (pinned to 25830f84bd)