Skip to content

ManagedWebSocket: ThrowIfInvalidMessageType: ArgumentException should report actual invalid input, not assume all invalids are 'Close' #129066

Closed

Description

@mattflaschen

Description

In ManagedWebSocket, ThrowIfInvalidMessageType enforces that only Text and Binary are passed.

The issue is that the ArgumentException (resource string) reports that any invalid message type was "Close".

It should report the actual invalid value that was passed, which could be Close or any other WebSocketMessageType. This could be an arbitrary invalid value, e.g. 999.

It is true that the only 3 valid types are currently Text, Binary, and Close.

I don't believe it should be hard-coded to Close, because:

  1. It is possible that WebSocket will add new message types in the future. This is a tricky place to have to update.
  2. It is possible to pass an invalid value (e.g. 999) by parsing or casting. I did this intentionally to test my code on invalid inputs, but it is conceivable it could happen accidentally.

Reproduction Steps

await _webSocket.SendAsync(
    buffer: buffer,
    messageType: (WebSocketMessageType) 999,
    endOfMessage: true,
    cancellationToken: cancellationToken
);

Expected behavior

System.ArgumentException: The message type '999' is not allowed for the 'SendAsync' operation. Valid message types are: 'Binary, Text'. To close the WebSocket, use the 'CloseOutputAsync' operation instead.  (Parameter 'messageType')

Actual behavior

System.ArgumentException: The message type 'Close' is not allowed for the 'SendAsync' operation. Valid message types are: 'Binary, Text'. To close the WebSocket, use the 'CloseOutputAsync' operation instead.  (Parameter 'messageType')

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Activity

  1. added
    untriaged
    on Jun 6, 2026
  2. dotnet-policy-service commented on Jun 6, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @karelz, @dotnet/ncl
    See info in area-owners.md if you want to be subscribed.

  3. added this to the 11.0.0 milestone on Jun 8, 2026
  4. removed
    untriaged
    on Jun 8, 2026
  5. locked and limited conversation to collaborators on Jul 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions