{"record":{"id":"f3602725c3ebe5ce","repo":"microsoft/aspire","slug":"resource-resource-name-uses-multiple-replicas-and-a","errorCode":null,"errorMessage":"Resource '{resource.Name}' uses multiple replicas and a persistent lifetime. These features do not work together.","messagePattern":"Resource '(.+?)' uses multiple replicas and a persistent lifetime\\. These features do not work together\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/Dcp/DcpNameGenerator.cs","lineNumber":81,"sourceCode":"            AddInstancesAnnotation(resource, builder.ToImmutable());\n        }\n    }\n\n    private static void AddInstancesAnnotation(IResource resource, ImmutableArray<DcpInstance> instances)\n    {\n        resource.Annotations.Add(new DcpInstancesAnnotation(instances));\n    }\n\n    private static void ThrowIfPersistentExecutableHasReplicas(IResource resource)\n    {\n        if (resource is not (ExecutableResource or ProjectResource))\n        {\n            return;\n        }\n\n        if (resource.GetReplicaCount() > 1 && resource.GetLifetimeType() == Lifetime.Persistent)\n        {\n            throw new InvalidOperationException($\"Resource '{resource.Name}' uses multiple replicas and a persistent lifetime. These features do not work together.\");\n        }\n    }\n\n    public (string Name, string Suffix) GetContainerName(IResource container)\n    {\n        var nameSuffix = container.GetLifetimeType() switch\n        {\n            Lifetime.Session => GetRandomNameSuffix(),\n            _ => GetProjectHashSuffix(),\n        };\n\n        return (GetObjectNameForResource(container, _options.Value, nameSuffix), nameSuffix);\n    }\n\n    public (string Name, string Suffix) GetExecutableName(IResource project)\n    {\n        var nameSuffix = project.GetLifetimeType() switch\n        {","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/Dcp/DcpNameGenerator.cs#L63-L99","documentation":"A resource configured with both a persistent lifetime (survives AppHost restarts, e.g. dcpdev / with persistent lifetime) and more than one replica is rejected: persistent resources are keyed to stable names/ports, which replicas cannot share. The check runs in ThrowIfPersistentExecutableHasReplicas when DCP instances are populated.","triggerScenarios":"Executable (or container) resource with Lifetime.Persistent (e.g. via WithLifetime(Lifetime.Persistent) or the dcpdev/persistent-lifetime feature) plus GetReplicaCount() > 1 from WithReplicas, during EnsureDcpInstancesPopulated.","commonSituations":"Combining the persistent-lifetime dev-loop feature (fast restarts) with scaled-out replicas; enabling persistent lifetime globally without realizing it conflicts with WithReplicas in some projects.","solutions":["Remove WithReplicas(n) (or set to 1) on resources using persistent lifetime.","Set the lifetime back to the default (e.g. WithLifetime(Lifetime.Session) or drop the persistent-lifetime flag) if replicas are needed.","Split the app: keep the persistent single instance for iteration and a separate session-lifetime resource for scaled scenarios."],"exampleFix":"// before\nbuilder.AddProject<Projects.Worker>(\"worker\")\n    .WithLifetime(Lifetime.Persistent)\n    .WithReplicas(3);\n// after\nbuilder.AddProject<Projects.Worker>(\"worker\")\n    .WithLifetime(Lifetime.Session)\n    .WithReplicas(3);","handlingStrategy":"validation","validationCode":"if (resource.GetReplicaCount() > 1 && resource.GetLifetimeType() == Lifetime.Persistent)\n    throw new InvalidOperationException($\"{resource.Name}: persistent lifetime is incompatible with multiple replicas.\");","typeGuard":null,"tryCatchPattern":"try\n{\n    app.Run();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"multiple replicas and a persistent lifetime\"))\n{\n    // drop replicas or switch back to session lifetime\n}","preventionTips":["Audit WithReplicas calls before enabling persistent lifetime globally (e.g. aspire run with persistent resources).","Document that persistent-lifetime resources must stay single-instance in team conventions.","Centralize lifetime configuration so replicas and lifetime are set together consistently."],"tags":["aspire","dcp","replicas","lifetime","persistent"],"backgroundTag":"conflicting-config-options","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"}