Skip to content

[API Proposal]: WebSocketException to expose the underlying HTTPResponse or the response status #62474

Description

@vicancy

Background and motivation

When customers connect to the Azure Web PubSub service using ClientWebSocket, there is a need to check if the connection failure is reconnectable or not. For example, if the HTTP upgrade request returns 400, this WebSocket connection is not reconnectable, while when the HTTP upgrade request returns 5xx, this might be caused by intermittent network issues and it is reconnectable. However, in the current design

throw new WebSocketException(WebSocketError.NotAWebSocket, SR.Format(SR.net_WebSockets_Connect101Expected, (int)response.StatusCode));
, the WebSocketException simply throws WebSocketError.NotAWebSocket which makes it hard for customers to differentiate from different scenarios. Customers have to parse the net_WebSockets_Connect101Expected string to get out the status code...

API Proposal

namespace System.Net.WebSockets
{
    public sealed class WebSocketException : Win32Exception
    {
        public HttpResponseMessage UpgradeResponse { get; }
    }
}

API Usage

try {
    // Start the WebSocket connection
} catch (WebSocketException e) {
   if (e.UpgradeResponse.StatusCode >= 500) {
      // Try reconnecting...
   }
}

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Net

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions