Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is 'using' disposing the instance in this case

I’m calling the following method in a loop while it is true, which leads to automatic reconnection. Since I’m using the using keyword, does it dispose the ClientWebSocket on each try/catch return false or it only disposes it if it reaches the end of ConnectAsync?

private async Task<bool> ConnectAsync(CancellationToken cancellationToken)
{
    using var clientWebSocket = new ClientWebSocket(); // TODO: Check if it's actually disposed
    _clientWebSocket = clientWebSocket;

    try
    {
        await clientWebSocket.ConnectAsync(new Uri(_url), cancellationToken);
    }
    catch
    {
        return false;
    }

    try
    {
        while (clientWebSocket.State == WebSocketState.Open && !cancellationToken.IsCancellationRequested)
        {
            var bytesReceived = new ArraySegment<byte>(new byte[1024]);
            var receiveResult = await clientWebSocket.ReceiveAsync(bytesReceived, cancellationToken);
            var message = Encoding.UTF8.GetString(bytesReceived.Array, 0, receiveResult.Count);
            
            ...
        }
    }
    catch
    {
        return false;
    }

    try
    {
        if (clientWebSocket.State == WebSocketState.Open)
        {
            await clientWebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "bye", CancellationToken.None);
        }
    }
    catch
    {
        return false;
    }

    return true;
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

using var clientWebSocket = new ClientWebSocket(); is just syntactical sugar, it’s short hand for:

private async Task<bool> ConnectAsync(CancellationToken cancellationToken)
{
    using( var clientWebSocket = new ClientWebSocket())
    {
        _clientWebSocket = clientWebSocket;

        try
        {
            await clientWebSocket.ConnectAsync(new Uri(_url), cancellationToken);
        }
        catch
        {
            return false;
        }

        try
        {
            while (clientWebSocket.State == WebSocketState.Open && !cancellationToken.IsCancellationRequested)
            {
                var bytesReceived = new ArraySegment<byte>(new byte[1024]);
                var receiveResult = await clientWebSocket.ReceiveAsync(bytesReceived, cancellationToken);
                var message = Encoding.UTF8.GetString(bytesReceived.Array, 0, receiveResult.Count);
                
                ...
            }
        }
        catch
        {
            return false;
        }

        try
        {
            if (clientWebSocket.State == WebSocketState.Open)
            {
                await clientWebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "bye", CancellationToken.None);
            }
        }
        catch
        {
            return false;
        }

        return true;

    }
}

which is short hand for:

private async Task<bool> ConnectAsync(CancellationToken cancellationToken)
{
    var clientWebSocket = new ClientWebSocket();
    try
    {
        _clientWebSocket = clientWebSocket;

        try
        {
            await clientWebSocket.ConnectAsync(new Uri(_url), cancellationToken);
        }
        catch
        {
            return false;
        }

        try
        {
            while (clientWebSocket.State == WebSocketState.Open && !cancellationToken.IsCancellationRequested)
            {
                var bytesReceived = new ArraySegment<byte>(new byte[1024]);
                var receiveResult = await clientWebSocket.ReceiveAsync(bytesReceived, cancellationToken);
                var message = Encoding.UTF8.GetString(bytesReceived.Array, 0, receiveResult.Count);
                
                ...
            }
        }
        catch
        {
            return false;
        }

        try
        {
            if (clientWebSocket.State == WebSocketState.Open)
            {
                await clientWebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "bye", CancellationToken.None);
            }
        }
        catch
        {
            return false;
        }

        return true;

    }
    finally
    {
        clientWebSocket.Dispose();
    }
}

I think it’s also worth highlighting this comment:

I’m more worried that you’re disposing something that you’ve assigned
to the field _clientWebSocket

clientWebSocket is going to get disposed, but because you assign _clientWebSocket to this (_clientWebSocket = clientWebSocket;), the object that varibale is pointing at will get disposed as well! They are the same thing. They reference each other. I’d imagine this is not what you want to happen here, but it’s hard to tell from the code you’ve shared.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading