microsoft/aspire · error · ArgumentNullException
ArgumentNullException for parameter 'resource' (resource is…
Error message
ArgumentNullException for parameter 'resource' (resource is null).
What it means
ConnectionStringRedirectAnnotation redirects connection-string resolution from one resource to another; it requires a non-null target resource implementing IResourceWithConnectionString. Passing null throws ArgumentNullException for parameter 'resource'.
Solutions
- Pass the actual resource, e.g. new ConnectionStringRedirectAnnotation(postgres)
- Ensure the referenced resource is created before adding the annotation and implements IResourceWithConnectionString
Example fix
// before
builder.Annotations.Add(new ConnectionStringRedirectAnnotation(null));
// after
var db = builder.AddPostgres("db");
builder.Annotations.Add(new ConnectionStringRedirectAnnotation(db.Resource)); Defensive patterns
Strategy: validation
Validate before calling
if (resource is null) throw new InvalidOperationException("Target resource must be created before adding ConnectionStringRedirectAnnotation."); Type guard
bool HasConnectionString(IResource? r) => r is IResourceWithConnectionString;
Prevention
- Create the referenced resource before adding the annotation
- Confirm the resource implements IResourceWithConnectionString
- Avoid casts to IResourceWithConnectionString without a null check
When it happens
Trigger: new ConnectionStringRedirectAnnotation(null) or passing a resource reference that is null (e.g. referencing a resource variable assigned later or a failed cast).
Common situations: Wiring annotations before the referenced resource is created; referencing a resource that does not implement IResourceWithConnectionString and being stored as null after a cast pattern.
Related errors
- ArgumentNullException for parameter 'callback' (callback is…
- ArgumentNullException for parameter 'resource' (resource is…
- description
- Object must be initialized before it can be used
- Object value cannot be null (ArgumentNullException…
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/ba3772c0d5405119.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Hosting/ApplicationModel/ConnectionStringRedirectAnnotation.cs:15
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Aspire.Hosting.ApplicationModel;
/// <summary>
/// Links to a resource that implements <see cref="IResourceWithConnectionString"/> that can be used by the containing resource to acquire a connection string.
/// </summary>
/// <param name="resource">Resource that </param>
public class ConnectionStringRedirectAnnotation(IResourceWithConnectionString resource) : IResourceAnnotation
{
/// <summary>
/// Callback to acquire connection string.
/// </summary>
public IResourceWithConnectionString Resource { get; } = resource ?? throw new ArgumentNullException(nameof(resource));
}
View on GitHub (pinned to 25830f84bd)