{"record":{"id":"0b422adbb02274bc","repo":"SignalR/SignalR","slug":"state-has-exceeded-the-maximum-length-of-4096-byte","errorCode":null,"errorMessage":"State has exceeded the maximum length of 4096 bytes.","messagePattern":"State has exceeded the maximum length of 4096 bytes\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.AspNet.SignalR.Core/Hubs/HubRequestParser.cs","lineNumber":60,"sourceCode":"            [JsonProperty(\"S\")]\n            public JRaw State { get; set; }\n            [JsonProperty(\"A\")]\n            public JRaw[] Args { get; set; }\n        }\n\n        private static IDictionary<string, object> GetState(HubInvocation deserializedData)\n        {\n            if (deserializedData.State == null)\n            {\n                return new Dictionary<string, object>();\n            }\n\n            // Get the raw JSON string and check if it's over 4K\n            string json = deserializedData.State.ToString();\n\n            if (json.Length > 4096)\n            {\n                throw new InvalidOperationException(Resources.Error_StateExceededMaximumLength);\n            }\n\n            var settings = JsonUtility.CreateDefaultSerializerSettings();\n            settings.Converters.Add(new SipHashBasedDictionaryConverter());\n            var serializer = JsonSerializer.Create(settings);\n            return serializer.Parse<IDictionary<string, object>>(json);\n        }\n    }\n}\n","sourceCodeStart":42,"sourceCodeEnd":70,"githubUrl":"https://github.com/SignalR/SignalR/blob/693053b89a9e1f5ce819e3233ed159a6409de22b/src/Microsoft.AspNet.SignalR.Core/Hubs/HubRequestParser.cs#L42-L70","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Reduce the data in hub state — pass large payloads as explicit method arguments instead of state","Move large data transfers to a separate REST/Web API call rather than embedding in SignalR state","Audit the client-side state object and prune unnecessary keys before each invocation"],"exampleFix":"// before (client)\n$.connection.hub.state = { bigData: hugeArray, cache: largeObject };\nserverHub.processData();\n\n// after (client)\nserverHub.processData(hugeArray); // pass as argument instead","handlingStrategy":"validation","validationCode":"// Client-side: check state size before invoking\nvar stateJson = JSON.stringify($.connection.hub.state || {});\nif (stateJson.length > 4096) {\n    console.warn('Hub state too large, trimming before invoke');\n    $.connection.hub.state = {};\n}","typeGuard":null,"tryCatchPattern":"try {\n    serverHub.processData();\n} catch (e) {\n    if (e.message.includes('maximum length')) {\n        // trim state and retry, or use a dedicated API call\n    }\n}","preventionTips":["Keep hub state minimal — use it for small key/value metadata only","Pass large payloads as explicit method arguments, not via the state dictionary","Periodically audit client-side state objects for accumulated data","Use a dedicated REST endpoint for bulk data transfer instead of SignalR state"],"tags":["signalr","hubs","state","size-limit","performance","client"],"backgroundTag":null,"analyzedSha":"693053b89a9e1f5ce819e3233ed159a6409de22b","analyzedAt":"2026-08-13T22:23:59.793Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}