socket.io-client-csharp
socket.io-client-csharp copied to clipboard
ReconnectError while awaiting ConnectAsync
During awaiting ConnectAsync i receive the following exception in the OnReconnectError Handler:
SocketIOClient.Transport.TransportException: Could not connect to 'https://mysockethost/socket.io/?EIO=3&transport=polling'
---> System.Text.Json.JsonException: JSON deserialization for type 'SocketIO.Serializer.SystemTextJson.JsonMessage' was missing required properties including: 'type'.
at System.Text.Json.ThrowHelper.ThrowJsonException_JsonRequiredPropertyMissing(JsonTypeInfo parent, BitArray requiredPropertiesSet)
at System.Text.Json.Serialization.Converters.ObjectWithParameterizedConstructorConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue)
at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, T& value, JsonSerializerOptions options, ReadStack& state)
at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Deserialize(Utf8JsonReader& reader, ReadStack& state)
at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo`1 jsonTypeInfo, Nullable`1 actualByteCount)
at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 json, JsonTypeInfo`1 jsonTypeInfo)
at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
at SocketIO.Serializer.SystemTextJson.SystemTextJsonSerializer.ReadOpenedMessage(IMessage message, String text)
at SocketIO.Serializer.SystemTextJson.SystemTextJsonSerializer.ReadMessage(IMessage message, EngineIO eio, String text)
at SocketIO.Serializer.SystemTextJson.SystemTextJsonSerializer.Deserialize(EngineIO eio, String text)
at SocketIOClient.Transport.BaseTransport.OnTextReceived(String text)
at SocketIOClient.Extensions.EventHandlerExtensions.TryInvokeAsync[T](Func`2 func, T arg1)
at SocketIOClient.Transport.Http.Eio3HttpPollingHandler.ProduceText(String text)
at SocketIOClient.Transport.Http.HttpPollingHandler.ProduceMessageAsync(HttpResponseMessage resMsg)
at SocketIOClient.Transport.Http.HttpPollingHandler.SendAsync(HttpRequestMessage req, CancellationToken cancellationToken)
at SocketIOClient.Transport.Http.HttpTransport.ConnectCoreAsync(Uri uri, CancellationToken cancellationToken)
at SocketIOClient.Transport.BaseTransport.ConnectAsync(CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at SocketIOClient.Transport.BaseTransport.ConnectAsync(CancellationToken cancellationToken)
at SocketIOClient.SocketIO.<>c__DisplayClass93_0.<<ConnectInBackground>b__0>d.MoveNext()
This suggests to me that there might be an issue in the protocol.
Took me a couple hours to figure this out by myself: As of .NET 9.0, constructor parameters are no longer considered optional. In my project configuration, I set the RespectRequiredConstructorParametersDefault to true. This likely caused a property that is not required by Socket.IO / Engine.IO protocol during the inital connect request (in this case the type property, that is missing in the connect request), to be considered as required in the JSON model for the JsonMessage type.
Removing this flag is a workaround, but does not fix the issue. I have to readjust all models in my entire codebase to match the feature flag. The model used by this library should explicitly configure if constructor parameters are required or optional.