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
Individual entries in the partitionKeyPaths collection are validated because Cosmos DB partition key paths must be non-empty strings (typically '/somePath'). Passing a collection containing null or an empty string produces an ArgumentException naming the partitionKeyPaths parameter.
Solutions
- Remove or fix null/empty entries before constructing: paths.Where(p => !string.IsNullOrEmpty(p)).ToArray().
- Ensure each path starts with '/' and is a real property path, e.g. '/address/zipCode'.
- Split configured strings with RemoveEmptyEntries: value.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).
- Validate input at the configuration boundary and throw your own descriptive error before reaching the resource constructor.
Example fix
// before
var paths = "/customerId,,".Split(','); // contains empty string
AddContainer(db, "orders", paths); // throws
// after
var paths = "/customerId".Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
AddContainer(db, "orders", paths); Defensive patterns
Strategy: validation
Validate before calling
if (paths.Any(string.IsNullOrEmpty))
{
throw new ArgumentException("All partition key paths must be non-empty strings starting with '/'.");
} Try / catch
try
{
AddContainer(db, "orders", paths);
}
catch (ArgumentException ex) when (ex.ParamName == "partitionKeyPaths")
{
logger.LogError(ex, "Invalid partition key path entry for container {Container}.", containerName);
throw;
} Prevention
- Split configured path strings with RemoveEmptyEntries and TrimEntries.
- Sanitize each path to start with '/'.
- Validate user/config-supplied paths at the boundary before constructing resources.
When it happens
Trigger: Calling the constructor with a list like new[] { "/id", "" } or containing null — e.g. from string.Split on an empty segment, or user-supplied path strings not trimmed/validated before building the list.
Common situations: Parsing a comma-separated partition key string like "/a,,/b" and splitting without filtering empty entries; config values with trailing commas; deserializing JSON paths where one entry is null.
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
- At least one partition key path should be provided.
- Address prefix must be a string or a parameter resource…
- Address prefix must be omitted, a string, or a parameter…
- Catalog name cannot be null or whitespace.
- 'ownerResource' and 'identityResource' must both be null…
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/cb0d51dd4e972ac5.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBContainerResource.cs:39
/// <summary>
/// Initializes a new instance of the <see cref="AzureCosmosDBContainerResource"/> class.
/// </summary>
/// <param name="name">The resource name.</param>
/// <param name="containerName">The container name.</param>
/// <param name="partitionKeyPaths">The hierarchical partition key paths.</param>
/// <param name="parent">The parent Azure Cosmos DB database resource.</param>
public AzureCosmosDBContainerResource(string name, string containerName, IEnumerable<string> partitionKeyPaths, AzureCosmosDBDatabaseResource parent) : base(name)
{
ArgumentException.ThrowIfNullOrEmpty(containerName);
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 (partitionKeyPaths.Any(string.IsNullOrEmpty))
{
throw new ArgumentException("Partition key paths cannot contain null or empty strings.", nameof(partitionKeyPaths));
}
ContainerProperties = new ContainerProperties(containerName, partitionKeyPathsArray);
Parent = parent ?? throw new ArgumentNullException(nameof(parent));
}
/// <summary>
/// Initializes a new instance of the <see cref="AzureCosmosDBContainerResource"/> class.
/// </summary>
/// <param name="name">The resource name.</param>
/// <param name="containerName">The container name.</param>
/// <param name="partitionKeyPath">The partition key path.</param>
/// <param name="parent">The parent Azure Cosmos DB database resource.</param>
public AzureCosmosDBContainerResource(string name, string containerName, string partitionKeyPath, AzureCosmosDBDatabaseResource parent) : base(name)
{
ArgumentException.ThrowIfNullOrEmpty(containerName);
ArgumentException.ThrowIfNullOrEmpty(partitionKeyPath);
ContainerProperties = new ContainerProperties(containerName, partitionKeyPath);View on GitHub (pinned to 25830f84bd)