doghappy / socket.io-client-csharp

socket.io-client implemention for .NET
MIT License
733 stars 126 forks source link

VSTHRD101 Avoid unsupported async delegates #330

Open zimbres opened 1 year ago

zimbres commented 1 year ago

I have this code:

socket.OnConnected += async (sender, e) =>
{
    await socket.EmitAsync("login", (ack) =>
    {
        var result = JsonNode.Parse(ack.GetValue(0).ToString());
        if (result["ok"].ToString() != "true")
        {
            _logger.LogError("Uptime Kuma login failure");
        }
    }, data);
};

This code generates this Warning on VS.

This code is inside in a try catch block, but when some socket connection fail, the program crash with Unhandled exception:

Unhandled exception. System.Net.WebSockets.WebSocketException (0x80004005): The WebSocket is in an invalid state ('CloseSent') for this operation. Valid states are: 'Open, CloseReceived'
   at System.Net.WebSockets.WebSocketValidate.ThrowIfInvalidState(WebSocketState currentState, Boolean isDisposed, WebSocketState[] validStates)
   at System.Net.WebSockets.ManagedWebSocket.SendAsync(ReadOnlyMemory`1 buffer, WebSocketMessageType messageType, WebSocketMessageFlags messageFlags, CancellationToken cancellationToken)
--- End of stack trace from previous location ---
   at SocketIOClient.Transport.WebSockets.DefaultClientWebSocket.SendAsync(Byte[] bytes, TransportMessageType type, Boolean endOfMessage, CancellationToken cancellationToken)
   at SocketIOClient.Transport.WebSockets.WebSocketTransport.SendAsync(TransportMessageType type, Byte[] bytes, CancellationToken cancellationToken)
   at SocketIOClient.Transport.WebSockets.WebSocketTransport.SendAsync(Payload payload, CancellationToken cancellationToken)
   at SocketIOClient.Transport.BaseTransport.SendAsync(IMessage msg, CancellationToken cancellationToken)
   at SocketIOClient.SocketIO.EmitAsync(String eventName, CancellationToken cancellationToken, Action`1 ack, Object[] data)
   at SocketIOClient.SocketIO.EmitAsync(String eventName, Action`1 ack, Object[] data)
   at UptimeKumaRemoteProbe.Services.MonitorsService.<>c__DisplayClass4_0.<<GetMonitorsAsync>b__1>d.MoveNext() in /src/Services/MonitorsService.cs:line 43
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_1(Object state)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()

Is there something that I can change in my code or the function EmitAsync need to be changed according to receive a Func action instead of an Action?

Thanks!