{"record":{"id":"6b02381e3f3bf694","repo":"dotnet/yarp","slug":"expected-to-receive-a-text-message-got-message","errorCode":null,"errorMessage":"Expected to receive a text message, got '{message.MessageType}' instead.","messagePattern":"Expected to receive a text message, got '(.+?)' instead\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"testassets/TestClient/Scenarios/WebSocketsScenario.cs","lineNumber":43,"sourceCode":"        await client.ConnectAsync(targetUri, cancellation);\n        Console.WriteLine($\"Channel established in {stopwatch.ElapsedMilliseconds} ms.\");\n\n        Console.WriteLine(\"Sending text messages...\");\n        var buffer = new byte[1024];\n        stopwatch.Restart();\n        for (var i = 0; i < 256; i++)\n        {\n            var textToSend = $\"Hello {i}\";\n            var numBytes = Encoding.UTF8.GetBytes(textToSend, buffer.AsSpan());\n            await client.SendAsync(new ArraySegment<byte>(buffer, 0, numBytes),\n                WebSocketMessageType.Text,\n                endOfMessage: true,\n                cancellation);\n\n            var message = await client.ReceiveAsync(buffer, cancellation);\n            if (message.MessageType != WebSocketMessageType.Text)\n            {\n                throw new Exception($\"Expected to receive a text message, got '{message.MessageType}' instead.\");\n            }\n\n            if (!message.EndOfMessage)\n            {\n                throw new Exception(\"Expected to receive EndOfMessage = true.\");\n            }\n\n            var text = Encoding.UTF8.GetString(buffer.AsSpan(0, message.Count));\n            if (text != textToSend)\n            {\n                throw new Exception($\"Expected to receive '{textToSend}', but got '{text}'.\");\n            }\n\n            Console.Write(\".\");\n        }\n\n        Console.WriteLine();\n        Console.WriteLine($\"Completed 256 text messages in {stopwatch.ElapsedMilliseconds} ms.\");","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/dotnet/yarp/blob/bd11867bee7df522e7fd3effb08a9c85fd616908/testassets/TestClient/Scenarios/WebSocketsScenario.cs#L25-L61","documentation":"Asserted in the first loop of WebSocketsScenario, which sends 256 text frames and expects each response frame to also be of type Text. If ReceiveAsync returns a different WebSocketMessageType (Binary, Close, etc.) it throws Exception with the actual type, signalling the echo endpoint altered the frame type or sent a control/close frame.","triggerScenarios":"Running WebSocketsScenario against a WebSocket endpoint that does not echo text frames as text, or that injects a Close/Pong frame mid-loop.","commonSituations":"Backend that normalizes all frames to binary; a half-open connection causing a Close frame; an intermediary that re-fragments frames; the endpoint sending an unsolicited close due to an error.","solutions":["Use the matching test backend that echoes text frames as text.","Confirm the WebSocket connection stayed open (a Close type usually means the peer closed).","Remove intermediaries that change frame types (some proxies convert text<->binary).","Log message.Count and MessageType together to distinguish a close from a type mismatch."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { await WebSocketsScenario.RunAsync(client, args, cancellation); }\ncatch (Exception ex) when (ex.Message.Contains(\"Expected to receive a text message\")) {\n    Console.Error.WriteLine(\"WebSocket peer did not echo a text frame; possibly a Close or type conversion.\");\n    return 1;\n}","preventionTips":["Use the matching WebSocket backend that echoes text frames as text.","A Close MessageType usually means the peer closed early; check server logs.","Remove proxies that convert text frames to binary."],"tags":["test","websockets","scenario","test-client","yarp"],"backgroundTag":null,"analyzedSha":"bd11867bee7df522e7fd3effb08a9c85fd616908","analyzedAt":"2026-08-13T21:29:49.359Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}