microsoft/aspire · error · ArgumentException
At least one partition key path should be provided.
Error message
At least one partition key path should be provided.
What it means
AddContainer requires at least one partition key path: the extension materializes the collection to an array and throws ArgumentException when it is empty, mirroring the resource-level validation. Cosmos DB containers cannot be defined without a partition key path.
Solutions
- Pass at least one non-empty partition key path such as "/id".
- Validate the collection is non-empty before calling AddContainer.
- Fix the upstream configuration so at least one path is populated.
Example fix
// before
var paths = config.GetPartitionKeyPaths(); // returns empty list
db.AddContainer("orders", paths);
// after
var paths = config.GetPartitionKeyPaths();
if (paths.Count == 0) throw new InvalidOperationException("At least one partition key path must be configured.");
db.AddContainer("orders", paths); Defensive patterns
Strategy: validation
Validate before calling
if (partitionKeyPaths is null || !partitionKeyPaths.Any()) throw new ArgumentException("At least one partition key path must be provided."); Try / catch
try { db.AddContainer("orders", paths); } catch (ArgumentException ex) when (ex.ParamName == "partitionKeyPaths") { /* fall back to "/id" and retry */ } Prevention
- Default to "/id" when no path is configured.
- Filter config-derived lists before calling, then check count.
- Fail fast on empty collections at configuration load time.
When it happens
Trigger: Calling builder.AddContainer(name, Array.Empty<string>()) or an empty List<string>; passing a lazily-evaluated enumerable that yields no items.
Common situations: Building the path list from config where no keys were configured; a LINQ filter that removes all entries before the call; default-initialized empty arrays.
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
- At least one partition key path should be provided.
- Partition key paths cannot contain null or empty strings.
- At least one terminal host is required.
- Cannot build connection string for MongoDB replica set…
- ConnectionStringAvailableEvent was published for the
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/430263cc8173afd1.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBExtensions.cs:443
/// <summary>
/// Adds a container to the associated Cosmos DB database resource with hierarchical partition keys.
/// </summary>
/// <param name="builder">CosmosDBDatabase resource builder.</param>
/// <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");
}
View on GitHub (pinned to 25830f84bd)