SignalR/SignalR · error · InvalidOperationException

State has exceeded the maximum length of 4096 bytes.

Error message

State has exceeded the maximum length of 4096 bytes.

What it means

Thrown when the client-side hub state dictionary (the 'S' field in the hub invocation JSON) exceeds 4096 characters when serialized to JSON. SignalR enforces this hard limit to prevent oversized per-invocation state from degrading performance. The state is the per-call caller-state object clients send alongside method invocations.

Source

Thrown at src/Microsoft.AspNet.SignalR.Core/Hubs/HubRequestParser.cs:60

            [JsonProperty("S")]
            public JRaw State { get; set; }
            [JsonProperty("A")]
            public JRaw[] Args { get; set; }
        }

        private static IDictionary<string, object> GetState(HubInvocation deserializedData)
        {
            if (deserializedData.State == null)
            {
                return new Dictionary<string, object>();
            }

            // Get the raw JSON string and check if it's over 4K
            string json = deserializedData.State.ToString();

            if (json.Length > 4096)
            {
                throw new InvalidOperationException(Resources.Error_StateExceededMaximumLength);
            }

            var settings = JsonUtility.CreateDefaultSerializerSettings();
            settings.Converters.Add(new SipHashBasedDictionaryConverter());
            var serializer = JsonSerializer.Create(settings);
            return serializer.Parse<IDictionary<string, object>>(json);
        }
    }
}

View on GitHub (pinned to 693053b89a)

Solutions

  1. Reduce the data in hub state — pass large payloads as explicit method arguments instead of state
  2. Move large data transfers to a separate REST/Web API call rather than embedding in SignalR state
  3. Audit the client-side state object and prune unnecessary keys before each invocation

Example fix

// before (client)
$.connection.hub.state = { bigData: hugeArray, cache: largeObject };
serverHub.processData();

// after (client)
serverHub.processData(hugeArray); // pass as argument instead
Defensive patterns

Strategy: validation

Validate before calling

// Client-side: check state size before invoking
var stateJson = JSON.stringify($.connection.hub.state || {});
if (stateJson.length > 4096) {
    console.warn('Hub state too large, trimming before invoke');
    $.connection.hub.state = {};
}

Try / catch

try {
    serverHub.processData();
} catch (e) {
    if (e.message.includes('maximum length')) {
        // trim state and retry, or use a dedicated API call
    }
}

Prevention

When it happens

Trigger: A JavaScript client sets $.connection.hub.state (or a per-hub state object) to a large structure before invoking a server method, and the serialized JSON of that state exceeds 4 KB.

Common situations: Storing large data structures (arrays, deep objects) in the SignalR state bag; migrating from query strings to state without size awareness; client-side state that accumulates entries over time without cleanup.

Related errors


AI-assisted analysis of SignalR/SignalR@693053b89a (2026-08-13). Data as JSON: /api/errors/0b422adbb02274bc. Report an issue: GitHub.