Open Closed

A cycle during the cache serialization #8143


User avatar
0
alexander.nikonov created
  • ABP Framework version: v8.1.3
  • UI Type: Angular
  • Database System: EF Core (Oracle)
  • Auth Server Separated OpenID Server

How to avoid this error or even a warning?

WRN (admin) A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 64. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles. Path: $.MyObject.Equipment.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Location.System.Text.Json.JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 64. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles. Path: $.MyObject.Equipment.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Component.Configurations.Location. at System.Text.Json.ThrowHelper.ThrowJsonException_SerializerCycleDetected(Int32 maxDepth) at System.Text.Json.Serialization.JsonConverter1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.Metadata.JsonPropertyInfo1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer) at System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.JsonConverter1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.Metadata.JsonTypeInfo1.Serialize(Utf8JsonWriter writer, T& rootValue, Object rootValueBoxed) at System.Text.Json.Serialization.Metadata.JsonTypeInfo1.SerializeAsObject(Utf8JsonWriter writer, Object rootValue) ... at Volo.Abp.Json.SystemTextJson.JsonConverters.ObjectToInferredTypesConverter.Write(Utf8JsonWriter writer, Object objectToWrite, JsonSerializerOptions options) at System.Text.Json.Serialization.JsonConverter1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.JsonConverter1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.Metadata.JsonTypeInfo1.Serialize(Utf8JsonWriter writer, T& rootValue, Object rootValueBoxed) at System.Text.Json.JsonSerializer.WriteString[TValue](TValue& value, JsonTypeInfo1 jsonTypeInfo) at Volo.Abp.Caching.Utf8JsonDistributedCacheSerializer.Serialize[T](T obj) at Volo.Abp.Caching.DistributedCache`2.<>c__DisplayClass51_0.<

Please make note that I already use this setting in my HttpApiHostModule:

private static void ConfigureSerialization(ServiceConfigurationContext context)
{
    context.Services.AddControllers().AddJsonOptions(x => x.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles);
}

But obviously it is not enough for the cache.

Another note is that I really have navigation properties in both cases and I do use them in the EntityFramework code. I just want to exclude circular from the consideration, i.e. if I take component.Configurations - I do not want to read Component from each Configuration and if take configuration.Component - I do not want further read Configurations of each Component.


3 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can try to configure the AbpSystemTextJsonSerializerOptions to set the JsonSerializerSettings

    https://abp.io/docs/latest/framework/infrastructure/json#abpsystemtextjsonserializeroptions

  • User Avatar
    0
    alexander.nikonov created

    hi

    You can try to configure the AbpSystemTextJsonSerializerOptions to set the JsonSerializerSettings

    https://abp.io/docs/latest/framework/infrastructure/json#abpsystemtextjsonserializeroptions

    Are you sure it's JsonSerializerSettings, not JsonSerializerOptions? Because as far as I understand, JsonSerializerSettings is related to Newtonsoft.Json, not System.Text.Json from Microsoft. And the exception is related to the latter.

    Besides, where do you suggest to place it?

        public class MyHttpApiModule : AbpModule
        {
            public override void ConfigureServices(ServiceConfigurationContext context)
            {
                ...
        
                Configure&lt;JsonOptions&gt;(options =>
                {
                    options.JsonSerializerOptions.Converters.Add(context.Services.GetRequiredService&lt;IStringToNullableIntConverter&gt;() as JsonConverter);
                    options.JsonSerializerOptions.Converters.Add(context.Services.GetRequiredService&lt;IStringToNullableLongConverter&gt;() as JsonConverter);
                    options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles; //Maybe this will help?
                });
        
                Configure&lt;AbpSystemTextJsonSerializerOptions&gt;(options =>
                {
                    //The change is here?
                });
            }
        }
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You are right. The property is JsonSerializerOptions

    https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptions.cs#L7

    You can configure it in your host module/project.

    eg: API.Host

Made with ❤️ on ABP v9.1.0-preview. Updated on December 05, 2024, 12:19