{"record":{"id":"630ff74a353dec7d","repo":"dotnet/yarp","slug":"session-affinity-is-disabled-for-cluster-cluster","errorCode":null,"errorMessage":"Session affinity is disabled for cluster {cluster.ClusterId}.","messagePattern":"Session affinity is disabled for cluster (.+?)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/ReverseProxy/SessionAffinity/BaseEncryptedSessionAffinityPolicy.cs","lineNumber":55,"sourceCode":"        if (context.RequestAborted.IsCancellationRequested)\n        {\n            // Avoid wasting time if the client is already gone.\n            return;\n        }\n\n        // Affinity key is set on the response only if it's a new affinity.\n        if (!context.Items.ContainsKey(AffinityKeyId))\n        {\n            var affinityKey = GetDestinationAffinityKey(destination);\n            SetAffinityKey(context, cluster, config, affinityKey);\n        }\n    }\n\n    public virtual AffinityResult FindAffinitizedDestinations(HttpContext context, ClusterState cluster, SessionAffinityConfig config, IReadOnlyList<DestinationState> destinations)\n    {\n        if (!config.Enabled.GetValueOrDefault())\n        {\n            throw new InvalidOperationException($\"Session affinity is disabled for cluster {cluster.ClusterId}.\");\n        }\n\n        var requestAffinityKey = GetRequestAffinityKey(context, cluster, config);\n\n        if (requestAffinityKey.Key is null)\n        {\n            return new AffinityResult(null, requestAffinityKey.ExtractedSuccessfully ? AffinityStatus.AffinityKeyNotSet : AffinityStatus.AffinityKeyExtractionFailed);\n        }\n\n        IReadOnlyList<DestinationState>? matchingDestinations = null;\n        if (destinations.Count > 0)\n        {\n            for (var i = 0; i < destinations.Count; i++)\n            {\n                // TODO: Add fast destination lookup by ID\n                if (requestAffinityKey.Key.Equals(GetDestinationAffinityKey(destinations[i])))\n                {\n                    // It's allowed to affinitize a request to a pool of destinations to enable load-balancing among them.","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/dotnet/yarp/blob/bd11867bee7df522e7fd3effb08a9c85fd616908/src/ReverseProxy/SessionAffinity/BaseEncryptedSessionAffinityPolicy.cs#L37-L73","documentation":"Thrown by BaseEncryptedSessionAffinityPolicy.FindAffinitizedDestinations when `config.Enabled` is not true. This is the read-side counterpart to AffinitizeResponse: looking up an affinity key is only valid when affinity is enabled for the cluster. The message includes `cluster.ClusterId` for diagnostics.","triggerScenarios":"FindAffinitizedDestinations is called on a cluster whose SessionAffinityConfig.Enabled is false/null. Happens when the affinity middleware runs against a cluster that did not opt into affinity.","commonSituations":"Affinity middleware added globally but only some clusters enable affinity. Cluster config edited and `Enabled` dropped. Test calling FindAffinitizedDestinations on a default config.","solutions":["Enable affinity on the cluster: `SessionAffinity = new SessionAffinityConfig { Enabled = true, ... }`.","Ensure the affinity middleware only runs for clusters with affinity enabled (YARP does this by default; custom wiring may not).","Confirm the ClusterState passed in matches an affinity-enabled cluster."],"exampleFix":"// before\nvar cfg = new SessionAffinityConfig { AffinityKeyName = \"Yarp.Affinity\" };\n// after\nvar cfg = new SessionAffinityConfig { Enabled = true, AffinityKeyName = \"Yarp.Affinity\" };","handlingStrategy":"validation","validationCode":"if (!config.Enabled.GetValueOrDefault())\n    throw new InvalidOperationException(\"Enable SessionAffinity before FindAffinitizedDestinations.\");","typeGuard":"static bool AffinityEnabled(SessionAffinityConfig? config) =>\n    config is not null && config.Enabled.GetValueOrDefault();","tryCatchPattern":null,"preventionTips":["Enable SessionAffinity on clusters that should use affinity lookups.","Ensure affinity middleware scopes itself to enabled clusters.","Add startup validation for SessionAffinityConfig consistency."],"tags":["session-affinity","config","yarp"],"backgroundTag":null,"analyzedSha":"bd11867bee7df522e7fd3effb08a9c85fd616908","analyzedAt":"2026-08-13T21:29:49.359Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}