1
I basically created an API project on. NET 5. My idea is to consume their API (Take repository information), and then make some information available in my API. The request is successful but there is always the same error in the part of converting a JSON to an object.
Repositoriescontroller:
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Threading.Tasks;
[ApiController]
[Route("[controller]")]
public class RepositoriesController : ControllerBase
{
private static readonly HttpClient client = new HttpClient();
private readonly ILogger<RepositoriesController> _logger;
public RepositoriesController(ILogger<RepositoriesController> logger)
{
_logger = logger;
}
[HttpGet]
public IActionResult Get(string number)
{
return Ok(ProcessRepositories());
}
private static async Task ProcessRepositories()
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
client.DefaultRequestHeaders.Add("User-Agent", ".NET Foundation Repository Reporter");
var streamTask = client.GetStreamAsync("https://api.github.com/orgs/dotnet/repos?sort=created&per_page=5&direction=desc");
var repositories = await JsonSerializer.DeserializeAsync<List<Repository>>(await streamTask);
}
}
Repository class:
public class Repository
{
public string full_name { get; set; }
public string node_id { get; set; }
//public string description { get; set; }
}
But always in the stretch:
var repositories = await JsonSerializer.DeserializeAsync<List<Repository>>(await streamTask);
The browser returns me the error:
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 32. Consider using Referencehandler.Preserve on Jsonserializeroptions to support Cycles.
I wanted to understand why only with two properties or even one ends up giving this error.
Documentation of the github api:
https://docs.github.com/en/rest/reference/repos
As a basis to understand how the API request works I used the official microsoft documentation: