dotnet / orleans

Cloud Native application framework for .NET
https://docs.microsoft.com/dotnet/orleans
MIT License
9.94k stars 2.02k forks source link

Change CanConvert method of JsonConverter(s) in OrleansJsonSerializer #8838

Open ArminShoeibi opened 5 months ago

ArminShoeibi commented 5 months ago

Hi there,

Recently, I encountered an exception of type Orleans.Runtime.OrleansException. I was in the process of adding available converters to my JSON serializer in Orleans, as demonstrated in the following code snippet:

            siloBuilder.Configure<OrleansJsonSerializerOptions>(options =>
            {
                options.JsonSerializerSettings.Converters.Add(new IPAddressConverter());
                options.JsonSerializerSettings.Converters.Add(new IPEndPointConverter());
            });
 Orleans.Runtime.OrleansException: Error from storage provider MemoryGrainStorage.database-hiccup-prs during WriteStateAsync for grain referenceid/Armin-Test-SQLServer-Wallet8

      Exc level 0: Newtonsoft.Json.JsonSerializationException: Error getting value from 'ScopeId' on 'System.Net.IPAddress+ReadOnlyIPAddress'.
         at Newtonsoft.Json.Serialization.ExpressionValueProvider.GetValue(Object target)
         at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)
         at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
         at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
         at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
         at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
         at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
         at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
         at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
         at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
         at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
         at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
         at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
         at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer)
         at Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Type type, JsonSerializerSettings settings)
         at Orleans.Serialization.OrleansJsonSerializer.Serialize(Object item, Type expectedType) in /_/src/Orleans.Core/Serialization/OrleansJsonSerializer.cs:line 51
         at Orleans.Storage.JsonGrainStorageSerializer.Serialize[T](T value) in /_/src/Orleans.Core/Providers/StorageSerializer/JsonGrainStorageSerializer.cs:line 24
         at Orleans.Storage.MemoryGrainStorage.ConvertToStorageFormat[T](T grainState) in /_/src/Orleans.Persistence.Memory/Storage/MemoryStorage.cs:line 173
         at Orleans.Storage.MemoryGrainStorage.WriteStateAsync[T](String grainType, GrainId grainId, IGrainState`1 grainState)
         at Orleans.Core.StateStorageBridge`1.WriteStateAsync() in /_/src/Orleans.Runtime/Storage/StateStorageBridge.cs:line 95
      Exc level 1: System.Net.Sockets.SocketException: The attempted operation is not supported for the type of object referenced.
         at System.Net.IPAddress.ThrowSocketOperationNotSupported()
         at System.Net.IPAddress.get_ScopeId()
         at lambda_method670(Closure, Object)
         at Newtonsoft.Json.Serialization.ExpressionValueProvider.GetValue(Object target)
       ---> Newtonsoft.Json.JsonSerializationException: Error getting value from 'ScopeId' on 'System.Net.IPAddress+ReadOnlyIPAddress'.
       ---> System.Net.Sockets.SocketException (10045): The attempted operation is not supported for the type of object referenced.
         at System.Net.IPAddress.ThrowSocketOperationNotSupported()
         at System.Net.IPAddress.get_ScopeId()
         at lambda_method670(Closure, Object)
         at Newtonsoft.Json.Serialization.ExpressionValueProvider.GetValue(Object target)
         --- End of inner exception stack trace ---
         at Newtonsoft.Json.Serialization.ExpressionValueProvider.GetValue(Object target)
         at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)
         at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
         at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
         at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
         at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
         at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
         at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
         at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
         at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
         at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
         at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
         at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
         at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer)
         at Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Type type, JsonSerializerSettings settings)
         at Orleans.Serialization.OrleansJsonSerializer.Serialize(Object item, Type expectedType) in /_/src/Orleans.Core/Serialization/OrleansJsonSerializer.cs:line 51
         at Orleans.Storage.JsonGrainStorageSerializer.Serialize[T](T value) in /_/src/Orleans.Core/Providers/StorageSerializer/JsonGrainStorageSerializer.cs:line 24
         at Orleans.Storage.MemoryGrainStorage.ConvertToStorageFormat[T](T grainState) in /_/src/Orleans.Persistence.Memory/Storage/MemoryStorage.cs:line 173
         at Orleans.Storage.MemoryGrainStorage.WriteStateAsync[T](String grainType, GrainId grainId, IGrainState`1 grainState)
         at Orleans.Core.StateStorageBridge`1.WriteStateAsync() in /_/src/Orleans.Runtime/Storage/StateStorageBridge.cs:line 95
         --- End of inner exception stack trace ---

However, I encountered an issue with these converters, which you can find detailed in the following links:

GitHub Issue Stack Overflow Answer After adding my custom converters and modifying the CanConvert method as suggested in the provided resources, I successfully resolved the exception. 😁

If you folks are okay I can make a PR and change those converters

wz172 commented 5 months ago

Add your own JsonSerializerSettings transformation way, AddNewtonsoftJsonSerializer expand method of overloading