microsoft/aspire · error · ArgumentNullException

ArgumentNullException for parameter 'resource' (resource is…

Error message

ArgumentNullException for parameter 'resource' (resource is null).

What it means

ConnectionStringReference represents a reference to another resource's connection string for use in expressions. It requires a non-null IResourceWithConnectionString; passing null throws ArgumentNullException for parameter 'resource'.

Solutions

  1. Pass the target resource implementing IResourceWithConnectionString, e.g. an AddPostgres/AddSqlServer result's Resource
  2. Fix the lookup/cast so the variable is non-null before constructing the reference
  3. Use builder.CreateConnectionStringReference or WithReference flows which validate the resource type

Example fix

// before
var reference = new ConnectionStringReference(null, optional: false);
// after
var db = builder.AddPostgres("db");
var reference = new ConnectionStringReference(db.Resource, optional: false);
Defensive patterns

Strategy: validation

Validate before calling

if (resource is null) throw new InvalidOperationException("Connection string resource not found.");

Type guard

bool IsConnectionStringResource(IResource? r) => r is IResourceWithConnectionString;

Try / catch

try { var reference = new ConnectionStringReference(resource, optional); } catch (ArgumentNullException) { /* resolve resource via builder before retrying */ }

Prevention

When it happens

Trigger: new ConnectionStringReference(null, optional: true/false), typically when the resource variable is null or derived from a failed lookup/cast.

Common situations: Building expression values in custom value providers where the target resource lookup returned null; referencing a resource that lacks a connection string.

Related errors


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

Appendix: source

Thrown at src/Aspire.Hosting/ApplicationModel/ConnectionStringReference.cs:13

// 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>
/// Represents a reference to a connection string.
/// </summary>
public class ConnectionStringReference(IResourceWithConnectionString resource, bool optional) : IExpressionValue, IManifestExpressionProvider, IValueProvider, IValueWithReferences
{
    /// <summary>
    /// The resource that the connection string is referencing.
    /// </summary>
    public IResourceWithConnectionString Resource { get; } = resource ?? throw new ArgumentNullException(nameof(resource));

    /// <summary>
    /// A flag indicating whether the connection string is optional.
    /// </summary>
    public bool Optional { get; } = optional;

    string IManifestExpressionProvider.ValueExpression => Resource.ValueExpression;

    IEnumerable<object> IValueWithReferences.References => [Resource];

    ValueTask<string?> IValueProvider.GetValueAsync(CancellationToken cancellationToken)
    {
        return Resource.GetValueAsync(cancellationToken);
    }

    async ValueTask<string?> IValueProvider.GetValueAsync(ValueProviderContext context, CancellationToken cancellationToken)
    {
        var value = await Resource.GetValueAsync(context, cancellationToken).ConfigureAwait(false);

View on GitHub (pinned to 25830f84bd)