dotnet/yarp · error · Exception

Expected to receive a text message, got '{message.MessageTyp

Error message

Expected to receive a text message, got '{message.MessageType}' instead.

What it means

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.

Source

Thrown at testassets/TestClient/Scenarios/WebSocketsScenario.cs:43

        await client.ConnectAsync(targetUri, cancellation);
        Console.WriteLine($"Channel established in {stopwatch.ElapsedMilliseconds} ms.");

        Console.WriteLine("Sending text messages...");
        var buffer = new byte[1024];
        stopwatch.Restart();
        for (var i = 0; i < 256; i++)
        {
            var textToSend = $"Hello {i}";
            var numBytes = Encoding.UTF8.GetBytes(textToSend, buffer.AsSpan());
            await client.SendAsync(new ArraySegment<byte>(buffer, 0, numBytes),
                WebSocketMessageType.Text,
                endOfMessage: true,
                cancellation);

            var message = await client.ReceiveAsync(buffer, cancellation);
            if (message.MessageType != WebSocketMessageType.Text)
            {
                throw new Exception($"Expected to receive a text message, got '{message.MessageType}' instead.");
            }

            if (!message.EndOfMessage)
            {
                throw new Exception("Expected to receive EndOfMessage = true.");
            }

            var text = Encoding.UTF8.GetString(buffer.AsSpan(0, message.Count));
            if (text != textToSend)
            {
                throw new Exception($"Expected to receive '{textToSend}', but got '{text}'.");
            }

            Console.Write(".");
        }

        Console.WriteLine();
        Console.WriteLine($"Completed 256 text messages in {stopwatch.ElapsedMilliseconds} ms.");

View on GitHub (pinned to bd11867bee)

Solutions

  1. Use the matching test backend that echoes text frames as text.
  2. Confirm the WebSocket connection stayed open (a Close type usually means the peer closed).
  3. Remove intermediaries that change frame types (some proxies convert text<->binary).
  4. Log message.Count and MessageType together to distinguish a close from a type mismatch.
Defensive patterns

Strategy: try-catch

Try / catch

try { await WebSocketsScenario.RunAsync(client, args, cancellation); }
catch (Exception ex) when (ex.Message.Contains("Expected to receive a text message")) {
    Console.Error.WriteLine("WebSocket peer did not echo a text frame; possibly a Close or type conversion.");
    return 1;
}

Prevention

When it happens

Trigger: Running WebSocketsScenario against a WebSocket endpoint that does not echo text frames as text, or that injects a Close/Pong frame mid-loop.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/yarp@bd11867bee (2026-08-13). Data as JSON: /api/errors/6b02381e3f3bf694. Report an issue: GitHub.