{"record":{"id":"a635f52b121f14bb","repo":"microsoft/aspire","slug":"unknown-persistence-mode-enum-getname-typeof-persistencemode","errorCode":null,"errorMessage":"Unknown persistence mode '{Enum.GetName(typeof(PersistenceMode), persistenceAnnotation.Mode)}'.","messagePattern":"Unknown persistence mode '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/ApplicationModel/ResourceExtensions.cs","lineNumber":1130,"sourceCode":"\n    private static Lifetime GetLifetimeType(IResource resource, HashSet<IResource> visitedResources)\n    {\n        if (!visitedResources.Add(resource))\n        {\n            throw new InvalidOperationException($\"A circular lifetime reference was detected for resource '{resource.Name}'.\");\n        }\n\n        if (resource.TryGetLastAnnotation<PersistenceAnnotation>(out var persistenceAnnotation))\n        {\n            return persistenceAnnotation.Mode switch\n            {\n                PersistenceMode.Session => Lifetime.Session,\n                PersistenceMode.Persistent => Lifetime.Persistent,\n                PersistenceMode.Resource => persistenceAnnotation.SourceResource is { } sourceResource\n                    ? GetLifetimeType(sourceResource, visitedResources)\n                    : throw new InvalidOperationException($\"Resource '{resource.Name}' has a resource persistence mode but no source resource.\"),\n                PersistenceMode.ParentProcess => Lifetime.Persistent,\n                _ => throw new InvalidOperationException($\"Unknown persistence mode '{Enum.GetName(typeof(PersistenceMode), persistenceAnnotation.Mode)}'.\")\n            };\n        }\n\n        if (resource.TryGetLastAnnotation<ContainerLifetimeAnnotation>(out var containerLifetimeAnnotation))\n        {\n            return containerLifetimeAnnotation.Lifetime switch\n            {\n                ContainerLifetime.Session => Lifetime.Session,\n                ContainerLifetime.Persistent => Lifetime.Persistent,\n                _ => throw new InvalidOperationException($\"Unknown container lifetime '{Enum.GetName(typeof(ContainerLifetime), containerLifetimeAnnotation.Lifetime)}'.\")\n            };\n        }\n\n        return Lifetime.Session;\n    }\n\n    /// <summary>\n    /// Determines whether the specified resource has a persistent lifetime.","sourceCodeStart":1112,"sourceCodeEnd":1148,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/ApplicationModel/ResourceExtensions.cs#L1112-L1148","documentation":"GetLifetimeType maps each PersistenceMode enum value to a Lifetime. If the annotation's Mode is not one of the known values (Session, Persistent, Resource, ParentProcess), the switch's default arm throws InvalidOperationException naming the unknown mode.","triggerScenarios":"A PersistenceAnnotation with an out-of-range or unrecognized PersistenceMode value — typically from a newer/older assembly mismatch where an enum value was added but this switch predates it, or from manual annotation construction with a cast int.","commonSituations":"Version skew between Aspire packages where one package writes a PersistenceMode value the hosting core doesn't know; unsafe casts like (PersistenceMode)42 in custom tooling.","solutions":["Upgrade all Aspire packages to the same version so PersistenceMode values align.","Avoid casting raw ints to PersistenceMode; use the enum members.","Inspect the annotation that carries the bad mode and correct it to a valid enum member."],"exampleFix":"// before\nannotation.Mode = (PersistenceMode)99; // invalid\n// after\nannotation.Mode = PersistenceMode.Persistent; // valid enum member","handlingStrategy":"try-catch","validationCode":"if (!Enum.IsDefined(typeof(PersistenceMode), pa.Mode)) throw new InvalidOperationException($\"Unknown PersistenceMode: {pa.Mode}\");","typeGuard":"bool IsKnown(PersistenceMode m) => Enum.IsDefined(typeof(PersistenceMode), m);","tryCatchPattern":"try { var lifetime = resource.GetLifetimeType(); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"Unknown persistence mode\")) { /* check package version alignment */ throw; }","preventionTips":["Keep all Aspire packages on the same version.","Never cast ints to PersistenceMode; use enum members.","Re-check enum handling after upgrading Aspire."],"tags":["aspire","enum","persistence","version-mismatch"],"backgroundTag":"invalid-enum-value","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"}