检测到可能的对象周期,不支持 [英] A possible object cycle was detected which is not supported

查看:82
本文介绍了检测到可能的对象周期,不支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这里有关于这个特定问题的问答,但是我的问题很少(我想).

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

这是我的模特班:

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

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

这是我的控制人:

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);
    }
}

我的存储库:

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

这是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;
        }
    }

这是我得到的错误:

处理请求时发生未处理的异常.JsonException:检测到可能的对象周期,但不支持该周期.这可能是由于循环造成的,也可能是由于对象深度大于最大允许深度32而引起的.System.Text.Json.ThrowHelper.ThrowInvalidOperationException_SerializerCycleDetected(int maxDepth)

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)

从其他问题中我发现,如果您具有其他表的引用,则可能会发生这种错误,但就我而言,不涉及其他表.这是我创建的第一个表,只是尝试使用get方法.

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.

推荐答案

_repo.GetRoles(); 未使用 await 关键字

var角色= _repo.GetRoles();

应该是

可变角色=等待_repo.GetRoles();

您的混合异步和非异步

未经过测试,但您应该得到jist

below untested but you should get the jist

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

原始答案为:

仅供参考-我的原始答案实际上是正确的....objectA是... Task ,因为那是从 _repo.GetRoles();

Original Answer was:

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

您的objectA具有对objectB的引用,而objectB引用了objectA.

your objectsA have references to objectB which references objectsA.

这只是在递归循环中结束,基本上永远不会结束尝试序列化.

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

可能存在对象循环

您也没有在可能出现异常的地方添加代码抛出JsonResult,所以看不到您使用的是什么序列化器.

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

此外,如果您使用的是newtonJson,则对复杂的支持对象甚至更受限制.

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

进一步-只是基于FYI的q出现在评论中非异步版本,因为您的主(控制器)线程不是异步的如果您不想像上面那样更改它或为了清楚起见

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;
    }
}

更新以获取更多指南

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

这篇关于检测到可能的对象周期,不支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆