问题
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