Serialization problems in . Net Core 3

Asked

Viewed 59 times

2

I am studying . NET Core API and having problems with relationships, where I get the error whenever I try to list all "calibrations" of "equipment".

JsonException: A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32.

System.Text.Json.ThrowHelper.ThrowInvalidOperationException_SerializerCycleDetected(int maxDepth)

When I remove: ".Include(c => c.Calibration)" from control, I receive json with null "calibration".

What I want is to get all the calibrations (Calibrated and Valid) of each equipment.

public class Equipamento
    {
        public int Id { get; set; }
        public string Nome { get; set; }
        public int TipoEquipamentoId { get; set; }
        public TipoEquipamento TipoEquipamento { get; set; }
        public decimal Valor { get; set; }
        public string NotaFiscal { get; set; }
        public DateTime Entrada { get; set; }
        public List<Calibracao> Calibracao { get; set; }
    }

public class Calibracao
    {
        public int Id { get; set; }
        public int EquipamentoId { get; set; }
        public Equipamento Equipamento { get; set; }
        public DateTime Calibrado { get; set; }
        public DateTime Validade { get; set; }
    }

public class TesteContext : DbContext
    {
        public TesteContext(DbContextOptions<TesteContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Calibracao>()
                .HasOne(p => p.Equipamento)
                .WithMany(b => b.Calibracao);
        }

        public DbSet<Equipamento> Equipamentos { get; set; }
        public DbSet<TipoEquipamento> TiposEquipamento { get; set; }
        public DbSet<Calibracao> Calibracoes { get; set; }
    }

// GET: api/Equipamento
        [HttpGet]
        public async Task<ActionResult<IEnumerable<Equipamento>>> GetEquipamentos()
        {
            return await _context.Equipamentos
                .Include(t => t.TipoEquipamento)
                .Include(c => c.Calibracao)
                .ToListAsync();
        }

inserir a descrição da imagem aqui

1 answer

3


The problem is not exactly in the relationship, but in the serialization, as you can see by its type (JsonException) and also the exposure of the serializer used (System.Text.Json).

Although it was recently placed as the standard serializer, the System.Text.Json is not fully ready to replace Json.NET (known as Newtonsoft). This specific problem will only be dealt with in . net 5 (next version of . net), as it is possible to see in this measure.

To solve the problem there are 2 alternatives:

I would say that it is safer to use the old serializer. To do this, add the package to your project Microsoft.AspNetCore.Mvc.Newtonsoftjson and configure it as follows:

services
    .AddMvc()
    .AddNewtonsoftJson(options => { options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; });

  • It worked perfectly. Now, can I bring the last or first "calibration" record of each "equipment"?

  • 1

    I would tell you to ask a new question about how to create this kind of "relationship" on Ef. In a short time you should have answers.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.