{"record":{"id":"18622a529cf02b90","repo":"microsoft/aspire","slug":"project-defaults-have-already-been-applied-to-resource","errorCode":null,"errorMessage":"Project defaults have already been applied to resource '{builder.Resource.Name}'. WithProjectDefaults can only be called once per resource, and AddProject, AddCSharpApp and AddDotnetProject already call it. Pass ProjectResourceOptions to the method that adds the resource instead.","messagePattern":"Project defaults have already been applied to resource '(.+?)'\\. WithProjectDefaults can only be called once per resource, and AddProject, AddCSharpApp and AddDotnetProject already call it\\. Pass ProjectResourceOptions to the method that adds the resource instead\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/ProjectResourceBuilderExtensions.cs","lineNumber":514,"sourceCode":"    {\n        ArgumentNullException.ThrowIfNull(builder);\n        ArgumentNullException.ThrowIfNull(options);\n\n        var projectMetadata = builder.Resource.GetProjectMetadata();\n\n        // Carries the per-endpoint state the wiring below needs, and marks the resource as\n        // \".NET-launched\" for core features such as the Rebuild command.\n        if (!builder.Resource.TryGetLastAnnotation<ProjectLaunchDefaultsAnnotation>(out var launchDefaults))\n        {\n            launchDefaults = new ProjectLaunchDefaultsAnnotation();\n            builder.WithAnnotation(launchDefaults);\n        }\n\n        // Applying the defaults twice is rejected rather than ignored because most of the settings and infrastructure\n        // added below are not idempotent.\n        if (!launchDefaults.TrySetAppliedProjectMetadata(projectMetadata))\n        {\n            throw new InvalidOperationException(\n                $\"Project defaults have already been applied to resource '{builder.Resource.Name}'. \" +\n                $\"{nameof(WithProjectDefaults)} can only be called once per resource, and {nameof(AddProject)}, \" +\n                $\"{nameof(AddCSharpApp)} and AddDotnetProject already call it. \" +\n                $\"Pass {nameof(ProjectResourceOptions)} to the method that adds the resource instead.\");\n        }\n\n        launchDefaults.BuildConfiguration =\n            builder.ApplicationBuilder.AppHostAssembly?.GetCustomAttribute<AssemblyConfigurationAttribute>()?.Configuration;\n\n        var launchConfigurationType = ProjectLaunchConfigurationFactory.GetLaunchConfigurationType(\n            builder.Resource,\n            projectMetadata);\n        builder.WithDebugSupport(\n            mode => ProjectLaunchConfigurationFactory.Create(builder.Resource, mode),\n            launchConfigurationType);\n\n        // File-based apps (a bare .cs file) are a .NET 10 SDK feature. The check lives here rather than in\n        // each Add* method so every .NET-launched resource gets it, and it is deferred to start time because","sourceCodeStart":496,"sourceCodeEnd":532,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/ProjectResourceBuilderExtensions.cs#L496-L532","documentation":"WithProjectDefaults throws InvalidOperationException when project launch defaults have already been applied to the resource's metadata. Defaults are deliberately not idempotent, so a second application (detected via TrySetAppliedProjectMetadata returning false) is rejected rather than ignored. AddProject, AddCSharpApp, and AddDotnetProject already call it internally, so calling WithProjectDefaults again — or applying defaults to a resource created another way twice — triggers this error.","triggerScenarios":"Explicitly calling WithProjectDefaults on a resource created by AddProject/AddCSharpApp/AddDotnetProject; or calling WithProjectDefaults twice on the same resource.","commonSituations":"Copy-pasted AppHost code that applies defaults manually after AddProject, migration of older AppHost code to the new defaults API, or shared helper methods that each call WithProjectDefaults.","solutions":["Remove the explicit WithProjectDefaults call — AddProject/AddCSharpApp/AddDotnetProject already apply defaults.","Pass settings via ProjectResourceOptions to the Add* method instead of applying defaults afterwards.","If using a resource type that does not auto-apply defaults, ensure WithProjectDefaults is called exactly once."],"exampleFix":"// before\nvar api = builder.AddProject<Projects.Api>(\"api\");\napi.WithProjectDefaults(); // throws: defaults already applied\n\n// after\nvar api = builder.AddProject<Projects.Api>(\"api\", options =>\n{\n    // configure launch settings via ProjectResourceOptions here\n});","handlingStrategy":"try-catch","validationCode":"// Only call WithProjectDefaults on resources that have not had defaults applied\n// (i.e. not created via AddProject/AddCSharpApp/AddDotnetProject).","typeGuard":null,"tryCatchPattern":"try\n{\n    resourceBuilder.WithProjectDefaults();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Project defaults have already been applied\"))\n{\n    logger.LogWarning(ex, \"Defaults already applied to {Resource}; skipping.\", resourceBuilder.Resource.Name);\n}","preventionTips":["Never call WithProjectDefaults on resources from AddProject/AddCSharpApp/AddDotnetProject.","Pass configuration via ProjectResourceOptions to the Add* method instead.","Centralize defaults application in one helper to avoid duplicate calls."],"tags":["projects","apphost","double-initialization"],"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"}