Skip to content

.NET Core 3.0-preview9-014004 :: System.Text.Json.JsonException #30870

@jonmikeli

Description

@jonmikeli

Issue Title

System.Text.Json.JsonException: A possible object cycle was detected which is not supported.

Description

The exception is raised when trying to serialize an Exception in:

  • UseExceptionHandler()
  • MiddleWare loaded by UseMiddleware()

I have not tried the serialization in other contexts.
Replacing the serialization call by another serializer (ex: Newtonsoft.Json) works properly.

Versions

.Net Core 3.0.100-preview9-014004

Code

var result = JsonSerializer.Serialize<Exception>(exception, new JsonSerializerOptions { WriteIndented = true });

Exception detail

Exception thrown: 'System.Text.Json.JsonException' in System.Text.Json.dll
Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware: Error: An exception was thrown attempting to execute the error handler.

System.Text.Json.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 0.
at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_SerializerCycleDetected(Int32 maxDepth)
at System.Text.Json.JsonSerializer.Write(Utf8JsonWriter writer, Int32 originalWriterDepth, Int32 flushThreshold, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.JsonSerializer.WriteCore(Utf8JsonWriter writer, PooledByteBufferWriter output, Object value, Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.WriteCore(PooledByteBufferWriter output, Object value, Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.WriteCoreString(Object value, Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options)
at NETCore3APITemplate.API.Startup.<>c__DisplayClass12_0.<b__3>d.MoveNext() in D:\Sources\JMI\Git\ALMTools-Templates-DotNetCore-Simple\Sources\WorkingSolution\NETCore3APITemplate\NETCore3APITemplate\NETCore3APITemplate.API\Startup.cs:line 147
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.HandleException(HttpContext context, ExceptionDispatchInfo edi)
Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer: Error: Connection ID "18230571301796315147", Request ID "8000000f-0002-fd00-b63f-84710c7967bb": An unhandled exception was thrown by the application.

Packages references

<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.ApiExplorer" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.0.0-preview8.19405.7" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="4.0.0-preview8.19405.7" />
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc2" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="5.0.0-rc2" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.0.0-rc2" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUi" Version="5.0.0-rc2" />

Hope it helps.

Bests

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions