Repository navigation
Description
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
Activity
leecow commentedon Sep 16, 2019
@ahsonkhan or @steveharter PTAL?
ahsonkhan commentedon Sep 16, 2019
@jonmikeli - can you share what is your .NET object model that you are trying to serialize? Are you actually trying to serialize the built-in
System.Exceptiontype?If your object model has cycles, then this is the expected behavior since cycles are not supported as part of this release.
https://github.com/dotnet/corefx/issues/38579
https://github.com/dotnet/corefx/issues/37786
jonmikeli commentedon Sep 16, 2019
Hi @ahsonkhan,
Thanks for your answer.
I was trying to serialize the built-in System.Exception type.
Knowing that the property InnerException is of type System.Exception too, I guess that is the reason of the error.
GGAlanSmithee commentedon Sep 17, 2019
sorry for hijacking this issue, but since it's the most recent one - @ahsonkhan you mentioned that cyclic reference handling is on the roadmap for vNext - https://github.com/dotnet/corefx/issues/38579#issuecomment-502955776 is there any available roadmap for this? thanks
ahsonkhan commentedon Sep 17, 2019
Yes, @GGAlanSmithee - see https://github.com/dotnet/corefx/issues/41002 where @jozkee is working on designing the feature. Your feedback, usage, and scenario would be helpful there.
No problem, please feel free to participate on that issue.
jozkee commentedon Jan 21, 2020
The preview version for
ReferenceHandlingfeature is here.Download the
System.Text.Jsonpreview package version5.0.0-alpha.1.20071.1from dotnet5 gallery (through Visual Studio's NuGet client):https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json
How to use it:
Set
ReferenceHandling.PreservetoReferenceHandlingproperty inJsonSerializerOptions.Here's the spec doc including samples and notes about many scenarios.
If you want to add feedback, feel free to leave a comment in https://github.com/dotnet/corefx/issues/41002.
danich93 commentedon May 13, 2020
@jozkee this doesn't seem to work when applied "globally" like so:
services.AddControllers().AddJsonOptions(options => options.JsonSerializerOptions.ReferenceHandling = ReferenceHandling.Preserve);jozkee commentedon May 13, 2020
@Ohmu93 That's probably because of a know ASP.NET issue where they were not honoring configuration of newly added properties to
JsonSerializerOptions.Can you try the workaround described in #30820 (comment)? Once 5.0 is relased, this should no longer be an issue.
You can also try upgrading your ASP.NET version to 5.0.0-preview4 since this was already fixed in dotnet/aspnetcore#21016.
Let me know if that solves your problem.
danich93 commentedon May 14, 2020
@jozkee Yep, that workaround does the trick, cheers.