diff --git a/src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/BrowserWebSockets/BrowserWebSocket.cs b/src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/BrowserWebSockets/BrowserWebSocket.cs index 25cecaf3e3d9d..4288444f2de37 100644 --- a/src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/BrowserWebSockets/BrowserWebSocket.cs +++ b/src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/BrowserWebSockets/BrowserWebSocket.cs @@ -463,24 +463,39 @@ public override void Abort() Dispose(); } - public override async Task CloseAsync(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken) + public override Task CloseAsync(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken) { _writeBuffer = null; ThrowIfNotConnected(); - await CloseAsyncCore(closeStatus, statusDescription, cancellationToken).ConfigureAwait(continueOnCapturedContext: true); - } - - private async Task CloseAsyncCore(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken) - { - ThrowOnInvalidState(State, WebSocketState.Connecting, WebSocketState.Open, WebSocketState.CloseReceived, WebSocketState.CloseSent); WebSocketValidate.ValidateCloseStatus(closeStatus, statusDescription); - _tcsClose = new TaskCompletionSource(); - _innerWebSocketCloseStatus = closeStatus; - _innerWebSocketCloseStatusDescription = statusDescription; - _innerWebSocket!.Invoke("close", (int)closeStatus, statusDescription); - await _tcsClose.Task.ConfigureAwait(continueOnCapturedContext: true); + try + { + ThrowOnInvalidState(State, WebSocketState.Connecting, WebSocketState.Open, WebSocketState.CloseReceived, WebSocketState.CloseSent); + } + catch (Exception exc) + { + return Task.FromException(exc); + } + + return CloseAsyncCore(closeStatus, statusDescription, cancellationToken); + } + + private Task CloseAsyncCore(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken) + { + try + { + _tcsClose = new TaskCompletionSource(); + _innerWebSocketCloseStatus = closeStatus; + _innerWebSocketCloseStatusDescription = statusDescription; + _innerWebSocket!.Invoke("close", (int)closeStatus, statusDescription); + return _tcsClose.Task; + } + catch (Exception exc) + { + return Task.FromException(exc); + } } public override Task CloseOutputAsync(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken) => throw new PlatformNotSupportedException(); diff --git a/src/libraries/System.Net.WebSockets.Client/tests/CloseTest.cs b/src/libraries/System.Net.WebSockets.Client/tests/CloseTest.cs index 0acdbf73cca10..0e50e787d5873 100644 --- a/src/libraries/System.Net.WebSockets.Client/tests/CloseTest.cs +++ b/src/libraries/System.Net.WebSockets.Client/tests/CloseTest.cs @@ -103,7 +103,6 @@ public async Task CloseAsync_CloseDescriptionIsMaxLength_Success(Uri server) [OuterLoop("Uses external server")] [ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))] - [ActiveIssue("https://github.com/dotnet/runtime/issues/45531", TestPlatforms.Browser)] public async Task CloseAsync_CloseDescriptionIsMaxLengthPlusOne_ThrowsArgumentException(Uri server) { string closeDescription = new string('C', CloseDescriptionMaxLength + 1);