{"record":{"id":"ba3772c0d5405119","repo":"microsoft/aspire","slug":"argumentnullexception-for-parameter-resource-resource-is-ba3772","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/ConnectionStringRedirectAnnotation.cs","lineNumber":15,"sourceCode":"// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT license.\n\nnamespace Aspire.Hosting.ApplicationModel;\n\n/// <summary>\n/// Links to a resource that implements <see cref=\"IResourceWithConnectionString\"/> that can be used by the containing resource to acquire a connection string.\n/// </summary>\n/// <param name=\"resource\">Resource that </param>\npublic class ConnectionStringRedirectAnnotation(IResourceWithConnectionString resource) : IResourceAnnotation\n{\n    /// <summary>\n    /// Callback to acquire connection string.\n    /// </summary>\n    public IResourceWithConnectionString Resource { get; } = resource ?? throw new ArgumentNullException(nameof(resource));\n}\n","sourceCodeStart":1,"sourceCodeEnd":17,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/ApplicationModel/ConnectionStringRedirectAnnotation.cs#L1-L17","documentation":"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'.","triggerScenarios":"new ConnectionStringRedirectAnnotation(null) or passing a resource reference that is null (e.g. referencing a resource variable assigned later or a failed cast).","commonSituations":"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.","solutions":["Pass the actual resource, e.g. new ConnectionStringRedirectAnnotation(postgres)","Ensure the referenced resource is created before adding the annotation and implements IResourceWithConnectionString"],"exampleFix":"// before\nbuilder.Annotations.Add(new ConnectionStringRedirectAnnotation(null));\n// after\nvar db = builder.AddPostgres(\"db\");\nbuilder.Annotations.Add(new ConnectionStringRedirectAnnotation(db.Resource));","handlingStrategy":"validation","validationCode":"if (resource is null) throw new InvalidOperationException(\"Target resource must be created before adding ConnectionStringRedirectAnnotation.\");","typeGuard":"bool HasConnectionString(IResource? r) => r is IResourceWithConnectionString;","tryCatchPattern":null,"preventionTips":["Create the referenced resource before adding the annotation","Confirm the resource implements IResourceWithConnectionString","Avoid casts to IResourceWithConnectionString without a null check"],"tags":["null-argument","annotation","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"}