Look at this particular line of code: using var ws = new ClientWebSocket()
in the class below. When is that object disposed taking into account that it uses using
and it is being used in a few methods more like SendAsync, ReceiveAsync, etc.? If it was in a regular method like Main(), it usually gets disposed at the closing branch, but in this case it's not disposed there. This class also doesn't inherit IDisposable
. I removed the unnecessary details from the class.
public class Client
{
private ClientWebSocket? _ws;
private async Task<bool> ConnectAsync(CancellationToken cancellationToken)
{
using var ws = new ClientWebSocket();
_ws = ws;
...
}
public async Task SendAsync(string data)
{
...
await _ws.SendAsync(dataBytes, WebSocketMessageType.Text, true, CancellationToken.None);
}
}