microsoft/aspire · error · ArgumentNullException
Value cannot be null. (Parameter 'parent')
Error message
Value cannot be null. (Parameter 'parent')
What it means
A container resource must belong to an AzureCosmosDBDatabaseResource; the multi-path constructor checks parent for null and throws ArgumentNullException with parameter name 'parent'. The Parent property drives model navigation, so a null parent is invalid by construction.
Solutions
- Pass a real AzureCosmosDBDatabaseResource, e.g. var db = cosmos.AddDatabase("mydb"); then create the container against db.
- If the database may be absent, check it for null before constructing the container and surface your own error.
- Prefer the fluent builder API (cosmos.AddDatabase(...).AddContainer(...)) which guarantees a valid parent.
Example fix
// before
AddContainer(db /* null */, "orders", paths);
// after
var db = cosmos.AddDatabase("mydb");
AddContainer(db, "orders", paths); Defensive patterns
Strategy: type-guard
Validate before calling
if (db is null)
{
throw new InvalidOperationException("Container requires a non-null parent AzureCosmosDBDatabaseResource.");
} Type guard
bool hasParent = db is AzureCosmosDBDatabaseResource;
Try / catch
try
{
var container = new AzureCosmosDBContainerResource(name, containerName, paths, db);
}
catch (ArgumentNullException ex) when (ex.ParamName == "parent")
{
logger.LogError(ex, "Parent database resource was null when creating container {Container}.", containerName);
throw;
} Prevention
- Null-check database lookups (FirstOrDefault) before using the result.
- Avoid null! casts on database references.
- Use cosmos.AddDatabase(...).AddContainer(...) fluent chains to guarantee a valid parent.
When it happens
Trigger: Directly invoking new AzureCosmosDBContainerResource(name, containerName, paths, null!) or passing a database variable that is null because an earlier lookup (e.g. builder.Resources.OfType<...>().FirstOrDefault()) found nothing.
Common situations: Using the null-forgiving operator on a nullable database reference; calling AddContainer with a database returned from a failed lookup; refactoring that removed database creation but left container creation.
Related errors
- Object value cannot be null (ArgumentNullException: parent)
- Object value cannot be null (ArgumentNullException: parent)
- Value cannot be null. (Parameter 'config')
- Value cannot be null. (Parameter 'parent')
- Value cannot be null. (Parameter 'parent')
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/3b99cf6cc23b8871.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBContainerResource.cs:43
/// <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);
Parent = parent ?? throw new ArgumentNullException(nameof(parent));
}
/// <summary>View on GitHub (pinned to 25830f84bd)