microsoft/aspire · error · ArgumentException

Partition key paths cannot contain null or empty strings.

Error message

Partition key paths cannot contain null or empty strings.

What it means

AddContainer validates that every partition key path in the collection is a non-empty, non-null string before creating the Azure Cosmos DB container resource. Cosmos DB requires valid JSON property paths (e.g. '/customerId') as partition keys, so null/empty entries would produce an invalid container definition that would fail later at provisioning time.

Solutions

  1. Inspect the partitionKeyPaths array for null/empty entries and remove or correct them
  2. Ensure each path starts with '/' and names a real JSON property in your documents
  3. If paths come from configuration, validate them before calling AddContainer or use a guard that filters entries

Example fix

// before
builder.AddDatabase("cosmos").AddContainer("orders", "/customerId,".Split(','));
// after
var paths = "/customerId".Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
builder.AddDatabase("cosmos").AddContainer("orders", paths);
Defensive patterns

Strategy: validation

Validate before calling

var paths = (partitionKeyPaths ?? throw new ArgumentNullException(nameof(partitionKeyPaths))).ToArray();
if (paths.Length == 0 || paths.Any(string.IsNullOrEmpty))
    throw new ArgumentException("All partition key paths must be non-empty.");

Type guard

static bool AreValidPartitionKeyPaths(string[]? paths) =>
    paths is { Length: > 0 } && paths.All(p => !string.IsNullOrEmpty(p) && p.StartsWith('/'));

Try / catch

try { builder.AddContainer("orders", name, paths); }
catch (ArgumentException ex) when (ex.ParamName == nameof(partitionKeyPaths)) { /* fix config and retry once */ }

Prevention

When it happens

Trigger: Calling AddContainer with a partitionKeyPaths array that includes null elements or empty strings, e.g. new[] { "/id", "" } or an array built by splitting an empty string.

Common situations: Reading partition key paths from configuration where values are missing; string.Split(',') on an empty config value yielding [""]; dynamically building paths in a loop with uninitialized entries.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBExtensions.cs:448

    /// <param name="name">Name of container resource.</param>
    /// <param name="partitionKeyPaths">Hierarchical partition key paths for the container.</param>
    /// <param name="containerName">The name of the container. If not provided, this defaults to the same value as <paramref name="name"/>.</param>
    /// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
    [AspireExportIgnore(Reason = "Polyglot AppHosts use the internal addContainer dispatcher export.")]
    public static IResourceBuilder<AzureCosmosDBContainerResource> AddContainer(this IResourceBuilder<AzureCosmosDBDatabaseResource> builder, [ResourceName] string name, IEnumerable<string> partitionKeyPaths, string? containerName = null)
    {
        ArgumentNullException.ThrowIfNull(builder);
        ArgumentException.ThrowIfNullOrEmpty(name);
        ArgumentNullException.ThrowIfNull(partitionKeyPaths);
        var partitionKeyPathsArray = partitionKeyPaths.ToArray();
        if (partitionKeyPathsArray.Length == 0)
        {
            throw new ArgumentException("At least one partition key path should be provided.", nameof(partitionKeyPaths));
        }

        if (partitionKeyPathsArray.Any(string.IsNullOrEmpty))
        {
            throw new ArgumentException("Partition key paths cannot contain null or empty strings.", nameof(partitionKeyPaths));
        }

        // Use the resource name as the container name if it's not provided
        containerName ??= name;

        var container = new AzureCosmosDBContainerResource(name, containerName, partitionKeyPaths, builder.Resource);

        builder.Resource.Containers.Add(container);

        return builder.ApplicationBuilder.AddResource(container)
            .WithIconName("Box");
    }

    /// <summary>
    /// Configures the Azure Cosmos DB resource to be deployed use the default SKU provided by Azure.
    /// </summary>
    /// <param name="builder">The builder for the Azure Cosmos DB resource.</param>
    /// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>

View on GitHub (pinned to 25830f84bd)