{"record":{"id":"8d21d0a178425b73","repo":"microsoft/aspire","slug":"argumentnullexception-for-parameter-resource-resource-is-8d21d0","errorCode":null,"errorMessage":"ArgumentNullException for parameter 'resource' (resource is null).","messagePattern":"ArgumentNullException for parameter 'resource' \\(resource is null\\)\\.","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/ApplicationModel/ConnectionStringReference.cs","lineNumber":13,"sourceCode":"// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT license.\nnamespace Aspire.Hosting.ApplicationModel;\n\n/// <summary>\n/// Represents a reference to a connection string.\n/// </summary>\npublic class ConnectionStringReference(IResourceWithConnectionString resource, bool optional) : IExpressionValue, IManifestExpressionProvider, IValueProvider, IValueWithReferences\n{\n    /// <summary>\n    /// The resource that the connection string is referencing.\n    /// </summary>\n    public IResourceWithConnectionString Resource { get; } = resource ?? throw new ArgumentNullException(nameof(resource));\n\n    /// <summary>\n    /// A flag indicating whether the connection string is optional.\n    /// </summary>\n    public bool Optional { get; } = optional;\n\n    string IManifestExpressionProvider.ValueExpression => Resource.ValueExpression;\n\n    IEnumerable<object> IValueWithReferences.References => [Resource];\n\n    ValueTask<string?> IValueProvider.GetValueAsync(CancellationToken cancellationToken)\n    {\n        return Resource.GetValueAsync(cancellationToken);\n    }\n\n    async ValueTask<string?> IValueProvider.GetValueAsync(ValueProviderContext context, CancellationToken cancellationToken)\n    {\n        var value = await Resource.GetValueAsync(context, cancellationToken).ConfigureAwait(false);","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/ApplicationModel/ConnectionStringReference.cs#L1-L31","documentation":"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'.","triggerScenarios":"new ConnectionStringReference(null, optional: true/false), typically when the resource variable is null or derived from a failed lookup/cast.","commonSituations":"Building expression values in custom value providers where the target resource lookup returned null; referencing a resource that lacks a connection string.","solutions":["Pass the target resource implementing IResourceWithConnectionString, e.g. an AddPostgres/AddSqlServer result's Resource","Fix the lookup/cast so the variable is non-null before constructing the reference","Use builder.CreateConnectionStringReference or WithReference flows which validate the resource type"],"exampleFix":"// before\nvar reference = new ConnectionStringReference(null, optional: false);\n// after\nvar db = builder.AddPostgres(\"db\");\nvar reference = new ConnectionStringReference(db.Resource, optional: false);","handlingStrategy":"validation","validationCode":"if (resource is null) throw new InvalidOperationException(\"Connection string resource not found.\");","typeGuard":"bool IsConnectionStringResource(IResource? r) => r is IResourceWithConnectionString;","tryCatchPattern":"try { var reference = new ConnectionStringReference(resource, optional); } catch (ArgumentNullException) { /* resolve resource via builder before retrying */ }","preventionTips":["Build references from typed builder results (AddPostgres etc.) rather than loose variables","Null-check resource lookups before constructing references","Use optional=true semantics deliberately instead of null resources"],"tags":["null-argument","connection-string"],"backgroundTag":"null-argument","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}