{"record":{"id":"05682ddf52c57937","repo":"dotnet/yarp","slug":"session-affinity-is-disabled-for-cluster","errorCode":null,"errorMessage":"Session affinity is disabled for cluster.","messagePattern":"Session affinity is disabled for cluster\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/ReverseProxy/SessionAffinity/BaseEncryptedSessionAffinityPolicy.cs","lineNumber":34,"sourceCode":"{\n    private readonly IDataProtector _dataProtector;\n    protected static readonly object AffinityKeyId = new object();\n    protected readonly ILogger Logger;\n\n    protected BaseEncryptedSessionAffinityPolicy(IDataProtectionProvider dataProtectionProvider, ILogger logger)\n    {\n        ArgumentNullException.ThrowIfNull(logger);\n        _dataProtector = dataProtectionProvider?.CreateProtector(GetType().FullName!) ?? throw new ArgumentNullException(nameof(dataProtectionProvider));\n        Logger = logger;\n    }\n\n    public abstract string Name { get; }\n\n    public void AffinitizeResponse(HttpContext context, ClusterState cluster, SessionAffinityConfig config, DestinationState destination)\n    {\n        if (!config.Enabled.GetValueOrDefault())\n        {\n            throw new InvalidOperationException($\"Session affinity is disabled for cluster.\");\n        }\n\n        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    {","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/dotnet/yarp/blob/bd11867bee7df522e7fd3effb08a9c85fd616908/src/ReverseProxy/SessionAffinity/BaseEncryptedSessionAffinityPolicy.cs#L16-L52","documentation":"Thrown by BaseEncryptedSessionAffinityPolicy.AffinitizeResponse when `config.Enabled` is not true (null or false). AffinitizeResponse writes the affinity key to the response, which is only meaningful when affinity is enabled; calling it on a disabled cluster is a programming/config error.","triggerScenarios":"The affinity policy's AffinitizeResponse is invoked on a cluster whose SessionAffinityConfig.Enabled is false or unset. Typically happens when the policy is wired but the cluster config did not enable affinity.","commonSituations":"Custom middleware or a test harness calls AffinitizeResponse directly. Cluster config partially migrated: policy registered but `SessionAffinity:Enabled` omitted. Config binding defaults Enabled to null.","solutions":["Set `SessionAffinity.Enabled = true` on the cluster config that uses an encrypted affinity policy (Cookie).","Guard the call site: only invoke AffinitizeResponse when `config.Enabled.GetValueOrDefault()` is true.","Verify the cluster's SessionAffinityConfig is the one you intend (check config binding)."],"exampleFix":"// before\nnew ClusterConfig { SessionAffinity = new SessionAffinityConfig { AffinityKeyName = \"Yarp.Affinity\" } };\n// after\nnew ClusterConfig { SessionAffinity = new SessionAffinityConfig { Enabled = true, AffinityKeyName = \"Yarp.Affinity\" } };","handlingStrategy":"validation","validationCode":"if (!config.Enabled.GetValueOrDefault())\n    throw new InvalidOperationException(\"Enable SessionAffinity before calling AffinitizeResponse.\");","typeGuard":"static bool AffinityEnabled(SessionAffinityConfig? config) =>\n    config is not null && config.Enabled.GetValueOrDefault();","tryCatchPattern":null,"preventionTips":["Always set SessionAffinity.Enabled=true when registering an encrypted affinity policy.","Guard custom AffinitizeResponse call sites with config.Enabled.","Validate cluster config at startup to catch disabled-affinity policies."],"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"}