return ConnectAsyncCore(uri, cancellationToken);
}
- private Task ConnectAsyncCore(Uri uri, CancellationToken cancellationToken)
+ private async Task ConnectAsyncCore(Uri uri, CancellationToken cancellationToken)
{
_innerWebSocket = new WebSocketHandle();
- // Change internal state to 'connected' to enable the other methods
- if ((InternalState)Interlocked.CompareExchange(ref _state, (int)InternalState.Connected, (int)InternalState.Connecting) != InternalState.Connecting)
+ try
+ {
+ await _innerWebSocket.ConnectAsync(uri, cancellationToken, Options).ConfigureAwait(false);
+ }
+ catch
{
- return Task.FromException(new ObjectDisposedException(nameof(ClientWebSocket))); // Aborted/Disposed during connect.
+ Dispose();
+ throw;
}
- return _innerWebSocket.ConnectAsync(uri, cancellationToken, Options);
+ if ((InternalState)Interlocked.CompareExchange(ref _state, (int)InternalState.Connected, (int)InternalState.Connecting) != InternalState.Connecting)
+ {
+ Debug.Assert(_state == (int)InternalState.Disposed);
+ throw new ObjectDisposedException(GetType().FullName);
+ }
}
public override Task SendAsync(ArraySegment<byte> buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken) =>
}
Assert.Equal(WebSocketState.Closed, cws.State);
Assert.Equal(exceptionMessage, ex.Message);
+
+ // Other operations throw after failed connect
+ await Assert.ThrowsAsync<ObjectDisposedException>(() => cws.ReceiveAsync(new byte[1], default));
+ await Assert.ThrowsAsync<ObjectDisposedException>(() => cws.SendAsync(new byte[1], WebSocketMessageType.Binary, true, default));
+ await Assert.ThrowsAsync<ObjectDisposedException>(() => cws.CloseAsync(WebSocketCloseStatus.NormalClosure, null, default));
+ await Assert.ThrowsAsync<ObjectDisposedException>(() => cws.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, null, default));
}
}