A possible object cycle was detected which is not supported

余生颓废 提交于 2020-03-05 04:29:42

问题


I know there are question and answers with that particular problem here on so, but my problem is little unique (I guess).

Here is my model class:

public class RoleMaster
{
    [Key]
    [Required]
    public int Id { get; set; }

    [Required]
    [StringLength(20)]        
    public string Role { get; set; }
}

Here is my controller:

public class RolesController : ControllerBase
{
    private readonly IRolesRepository _repo;


    public RolesController(IRolesRepository repo)
    {
        _repo = repo;
    }


    [HttpGet]
    public IActionResult Get()
    {
        var roles = _repo.GetRoles();
        return new JsonResult(roles);
    }
}

My repository:

public interface IRolesRepository
{
    Task<IEnumerable<RoleMaster>> GetRoles();
}

and here is the GetRoles method:

public async Task<IEnumerable<RoleMaster>> GetRoles()
    {
        try
        {
            var roles = await db.RoleMaster.AsNoTracking().ToListAsync();

            return roles;
        }
        catch (Exception)
        {
            throw;
        }
    }

and this is the error I am getting:

An unhandled exception occurred while processing the request. 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)

From other questions I found out that if you have references with other tables then this kind of error can occur but in my case there is no other table involved. This is the first table I have created and was just trying the get method.


回答1:


_repo.GetRoles(); is not using await keyword

this var roles = _repo.GetRoles();

should be

var roles = await _repo.GetRoles();

your mixing async and non async,

below untested but you should get the jist

  [HttpGet]
    public  async Task<IActionResult> Get()
    {
        var roles = **await** _repo.GetRoles();
        return new JsonResult(roles);
    }

Original Answer was:

FYI - my original answer was actually correct.... objectA was... Task, as that is what was being returned from _repo.GetRoles();

your objectsA have references to objectB which references objectsA.

this just end in a recursion loop, basically never end trying to serialize.

hense possible object cycle

Also you didn't include the code where exception is possible being throw JsonResult so can t not see what serializer you using.

Further if you are using newtonJson then the support for complex object is even more limited.

Further - Just an FYI based q's in on comments the non async version, as your main(controller) thread is not async if you don't want to change it, as above or for clearity

public List<RoleMaster> GetRoles()
{
    try
    {
        var roles =  db.RoleMaster.AsNoTracking().ToList();
        return roles;
    }
    catch (Exception)
    {
        throw;
    }
}

Update for more guidance

https://docs.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-3.1




回答2:


I had the same problem.

This is what solved my problem:

Putting JsonIgnore attribute on the referencing property (which caused endless serialization)



来源:https://stackoverflow.com/questions/59663388/a-possible-object-cycle-was-detected-which-is-not-supported

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!