dotnet/yarp · error · InvalidOperationException
Session affinity is disabled for cluster.
Error message
Session affinity is disabled for cluster.
What it means
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.
Source
Thrown at src/ReverseProxy/SessionAffinity/BaseEncryptedSessionAffinityPolicy.cs:34
{
private readonly IDataProtector _dataProtector;
protected static readonly object AffinityKeyId = new object();
protected readonly ILogger Logger;
protected BaseEncryptedSessionAffinityPolicy(IDataProtectionProvider dataProtectionProvider, ILogger logger)
{
ArgumentNullException.ThrowIfNull(logger);
_dataProtector = dataProtectionProvider?.CreateProtector(GetType().FullName!) ?? throw new ArgumentNullException(nameof(dataProtectionProvider));
Logger = logger;
}
public abstract string Name { get; }
public void AffinitizeResponse(HttpContext context, ClusterState cluster, SessionAffinityConfig config, DestinationState destination)
{
if (!config.Enabled.GetValueOrDefault())
{
throw new InvalidOperationException($"Session affinity is disabled for cluster.");
}
if (context.RequestAborted.IsCancellationRequested)
{
// Avoid wasting time if the client is already gone.
return;
}
// Affinity key is set on the response only if it's a new affinity.
if (!context.Items.ContainsKey(AffinityKeyId))
{
var affinityKey = GetDestinationAffinityKey(destination);
SetAffinityKey(context, cluster, config, affinityKey);
}
}
public virtual AffinityResult FindAffinitizedDestinations(HttpContext context, ClusterState cluster, SessionAffinityConfig config, IReadOnlyList<DestinationState> destinations)
{View on GitHub (pinned to bd11867bee)
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).
Example fix
// before
new ClusterConfig { SessionAffinity = new SessionAffinityConfig { AffinityKeyName = "Yarp.Affinity" } };
// after
new ClusterConfig { SessionAffinity = new SessionAffinityConfig { Enabled = true, AffinityKeyName = "Yarp.Affinity" } }; Defensive patterns
Strategy: validation
Validate before calling
if (!config.Enabled.GetValueOrDefault())
throw new InvalidOperationException("Enable SessionAffinity before calling AffinitizeResponse."); Type guard
static bool AffinityEnabled(SessionAffinityConfig? config) =>
config is not null && config.Enabled.GetValueOrDefault(); Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Session affinity is disabled for cluster {cluster.ClusterId}
- Session affinity is disabled for cluster.
- Session affinity is disabled for cluster {cluster.ClusterId}
- Unknown transform: {string.Join(';', rawTransform.Keys)}
- The transform contains more parameters than expected: {strin
AI-assisted analysis of dotnet/yarp@bd11867bee (2026-08-13).
Data as JSON: /api/errors/05682ddf52c57937.
Report an issue: GitHub.