Error "A possible object cycle was detected" in Mercury / .NET Core API project

Scenario:
Create a .NET Core API project using Mercury. When starting debug, I get the following error:

System.Text.Json.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.

The same project template with Oxygen works ok.

this is just with the project from template, no changes?

Yes, without any changes, other than setting a different port in Launchsettings.json.

Logged as bugs://E25373.

Do I need further steps to hit this error? the server ap launches fine for me and without exception:

can I get a full screenshot for this, and/or maybe a call stack ? DO I understand correctly is this an runtime exception in the app, or is it an error reported by the debugger/IDE? If the latter, are you using Fire, Water or VS? Thanx!

bugs://E25373 was closed as fixed.

Weird. I just tried to reproduce it again and couldn’t. After doing some further testing I found out that it has something to do with the port issue again. On a certain port, that was used earlier and thus probably in use / blocked, this Json came up. Once I changed the port to something new (not in use), the error was gone …

An exception occurred in WebApplication6, thread 0186

Type: System.Text.Json.JsonException

Message:

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.

Call Stack:

0000000000000000 System.Text.Json.ThrowHelper.ThrowJsonException_SerializerCycleDetected
0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite 0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson
0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite 0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite
0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson 0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite
0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite 0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson
0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite 0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite
0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson 0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite
0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite 0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson
0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite 0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite
0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson 0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite
0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite 0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson
0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite 0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite
0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson 0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite
0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite 0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson
0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite 0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite
0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson 0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite
0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite 0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson
0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite 0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite
0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson 0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite
0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite 0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson
0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite 0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite
0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson 0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite
0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite 0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson
0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite 0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite
0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson 0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite
0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite 0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson
0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite 0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite
0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson 0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite
0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite 0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson
0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite 0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite
0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson 0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite
0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite 0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson
0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite 0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite
0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson 0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite
0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite 0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson
0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite 0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite
0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson 0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite
0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite 0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson
0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite 0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite
0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson 0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite
0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite 0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson
0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite 0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite
0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson 0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite
0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite 0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson
0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite 0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite
0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson 0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite
0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite 0000000000000000 System.Text.Json.JsonPropertyInfo1.GetMemberAndWriteJson
0000000000000000 System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite 0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite
0000000000000000 System.Text.Json.Serialization.Converters.ArrayConverter2.OnWriteResume 0000000000000000 System.Text.Json.Serialization.Converters.IEnumerableDefaultConverter2.OnTryWrite
0000000000000000 System.Text.Json.Serialization.JsonConverter1.TryWrite 0000000000000000 System.Text.Json.Serialization.JsonConverter1.WriteCore
0000000000000000 System.Text.Json.Serialization.JsonConverter`1.WriteCoreAsObject
0000000000000000 System.Text.Json.JsonSerializer.WriteCore

This is the Diagnostic log. The error occurs after I let Kestrel output the Json. In other words, if I start debugging, the error doesn’t show up immediately, if it starts with the base URL (localhost:). If I add “/weatherforecast” and thus let the Controller create the Json output, the error comes up.

~> Process WebApplication6 started, took 0.723.
~> Attaching to 17473
~> Microsoft.Hosting.Lifetime: Information: Now listening on: http://localhost:5056
e[40me[32minfoe[39me[22me[49m: Microsoft.Hosting.Lifetime[0]
Now listening on: http://localhost:5056
~> Microsoft.Hosting.Lifetime: Information: Application started. Press Ctrl+C to shut down.
e[40me[32minfoe[39me[22me[49m: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
e[40me[32minfoe[39me[22me[49m: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
~> Microsoft.Hosting.Lifetime: Information: Hosting environment: Development
~> Microsoft.Hosting.Lifetime: Information: Content root path: /Users/marcvanbreemen/Lokaal/WebApplication6
e[40me[32minfoe[39me[22me[49m: Microsoft.Hosting.Lifetime[0]
Content root path: /Users/marcvanbreemen/Lokaal/WebApplication6
e[40me[32minfoe[39me[22me[49m: Microsoft.AspNetCore.Hosting.Diagnostics[1]
Request starting HTTP/1.1 GET http://localhost:5056/weatherforecast - -
~> Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request starting HTTP/1.1 GET http://localhost:5056/weatherforecast - -
e[40me[1me[33mwarne[39me[22me[49m: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
Failed to determine the https port for redirect.
~> Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware: Warning: Failed to determine the https port for redirect.
e[40me[32minfoe[39me[22me[49m: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
Executing endpoint ‘WebApplication6.Controllers.WeatherForecastController.Get (WebApplication6)’
~> Microsoft.AspNetCore.Routing.EndpointMiddleware: Information: Executing endpoint ‘WebApplication6.Controllers.WeatherForecastController.Get (WebApplication6)’
e[40me[32minfoe[39me[22me[49m: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[3]
Route matched with {action = “Get”, controller = “WeatherForecast”}. Executing controller action with signature System.Collections.Generic.IEnumerable1[WebApplication6.WeatherForecast] Get() on controller WebApplication6.Controllers.WeatherForecastController (WebApplication6). ~> Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Information: Route matched with {action = "Get", controller = "WeatherForecast"}. Executing controller action with signature System.Collections.Generic.IEnumerable1[WebApplication6.WeatherForecast] Get() on controller WebApplication6.Controllers.WeatherForecastController (WebApplication6).
e[40me[32minfoe[39me[22me[49m: Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor[1]
Executing ObjectResult, writing value of type ‘WebApplication6.WeatherForecast[]’.
~> Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor: Information: Executing ObjectResult, writing value of type ‘WebApplication6.WeatherForecast[]’.
!> Exception of type System.Text.Json.JsonException on thread 0186
!> Message: 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.

#> (edb)

I’m using Fire on macOS Monterey.

Hi, please, in WeatherForecast.vb file, add:
Imports System

on top of the file, should be:

Imports System

Public Class WeatherForecast

  Public Property [Date] As DateTime

  Public Property TemperatureC As Integer

  Public Property TemperatureF As Integer
    Get
      return 32 + Integer(TemperatureC / 0.5556)
    End Get
  End Property

  Public Property Summary As String

End Class

this fixes the issue.