{"record":{"id":"ccdc16fdf9457359","repo":"microsoft/aspire","slug":"resource-resource-name-project-metadata-was-replaced-after","errorCode":null,"errorMessage":"Resource '{resource.Name}' project metadata was replaced after project defaults were applied. Project metadata must remain unchanged after WithProjectDefaults configures the resource.","messagePattern":"Resource '(.+?)' project metadata was replaced after project defaults were applied\\. Project metadata must remain unchanged after WithProjectDefaults configures the resource\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/ApplicationModel/ProjectLaunchDefaultsAnnotation.cs","lineNumber":63,"sourceCode":"    internal bool TrySetAppliedProjectMetadata(IProjectMetadata projectMetadata)\n    {\n        if (_appliedProjectMetadata is not null)\n        {\n            return false;\n        }\n\n        _appliedProjectMetadata = projectMetadata;\n        return true;\n    }\n\n    /// <summary>\n    /// Validates that project metadata has not changed since project defaults were materialized.\n    /// </summary>\n    internal void ValidateProjectMetadata(IResource resource, IProjectMetadata projectMetadata)\n    {\n        if (_appliedProjectMetadata is not null && !ReferenceEquals(projectMetadata, _appliedProjectMetadata))\n        {\n            throw new InvalidOperationException(\n                $\"Resource '{resource.Name}' project metadata was replaced after project defaults were applied. \" +\n                $\"Project metadata must remain unchanged after {nameof(ProjectResourceBuilderExtensions.WithProjectDefaults)} configures the resource.\");\n        }\n    }\n}\n\ninternal static class ProjectLaunchDefaultsExtensions\n{\n    /// <summary>\n    /// Determines whether endpoint environment variables should be injected for the given endpoint.\n    /// Only http/https endpoints without an explicit target-port environment variable are eligible,\n    /// and any <see cref=\"EndpointEnvironmentInjectionFilterAnnotation\"/> may further exclude them.\n    /// </summary>\n    [AspireExportIgnore(Reason = \"Endpoint environment injection filtering is internal .NET launch wiring and is not part of the ATS surface.\")]\n    public static bool ShouldInjectEndpointEnvironment(this IResource resource, EndpointReference e)\n    {\n        var endpoint = e.EndpointAnnotation;\n","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/ApplicationModel/ProjectLaunchDefaultsAnnotation.cs#L45-L81","documentation":"ProjectLaunchDefaultsAnnotation captures a reference to the IProjectMetadata instance present when WithProjectDefaults ran and re-validates it later via ValidateProjectMetadata. If the resource's project metadata annotation was replaced by a different instance, the defaults may no longer correspond to the project, so it throws InvalidOperationException.","triggerScenarios":"Calling WithProjectDefaults and then adding/removing/replacing the resource's IProjectMetadata annotation (e.g. re-adding metadata with a different ProjectPath) before launch configuration resolution.","commonSituations":"Custom builder extensions that unconditionally AddAnnotation a new ProjectMetadata, duplicating or replacing the original; rebuilding a resource and re-applying metadata; copying annotations between resources.","solutions":["Do not replace IProjectMetadata after WithProjectDefaults; configure the project path before defaults are applied","If metadata must change, apply WithProjectDefaults again after the change (recreating the defaults annotation)","Use TryGetAnnotations to detect existing metadata before adding a new one"],"exampleFix":"// before\nbuilder.WithAnnotation(new ProjectMetadata { ProjectPath = otherPath }); // after defaults applied\n// after\n// set the project path BEFORE calling WithProjectDefaults\nbuilder.WithAnnotation(new ProjectMetadata { ProjectPath = otherPath });\nbuilder.WithProjectDefaults();","handlingStrategy":"validation","validationCode":"// after WithProjectDefaults, verify metadata is still the applied instance\nannotation.ValidateProjectMetadata(resource, metadata); // call defensively before launch resolution","typeGuard":null,"tryCatchPattern":"try { annotation.ValidateProjectMetadata(resource, metadata); } catch (InvalidOperationException) { /* re-apply WithProjectDefaults */ }","preventionTips":["Set project metadata before calling WithProjectDefaults and never touch it afterwards","Avoid unconditional AddAnnotation of ProjectMetadata in custom extensions"],"tags":["project-metadata","invalid-state","aspire-hosting"],"backgroundTag":"invalid-state-transition","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"}