Asp.NET Core 3.0 Web API “A Possible Object Cycle Was Detected Which Is Not Supported” Hatası Çözümü

Sayfanın alt kısmında çıktısını paylaştığım “A Possible Object Cycle Was Detected Which Is Not Supported” hatasını alıyorsanız bu yazı size göre. ASP.NET Core 3.0 ve üzeri sürümlerde*, JSON.NET bağımlılığını kaldırmıştır ve kendi JSON serileştiricisini yani ‘System.Text.Json’ kullanır. ReferenceLoopHandling şu anda System.Text.Json serileştiricisinde desteklenmemektedir . Bu özellik gelecekte en olası .NET 5 sürümünde desteklenecektir.

Ama bu sorunu aşağıdaki çözümlerden birini uygulayarak aşmak mümkün.

1.Çözüm

Microsoft.AspNetCore.Mvc.NewtonsoftJson pakedini yükleyin ve sonrasında Startup.cs class’ınızın içerisinde ConfigureServices methoduna aşağıdaki kod bloğunu ekleyebilirsiniz.

services.AddControllerWithViews()
.AddNewtonsoftJson(options =>
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);

2. Çözüm

Yine Microsoft.AspNetCore.Mvc.NewtonsoftJson pakedini yükleyin ve Startup.cs class’ınızın içerisinde ConfigureServices methoduna aşağıdaki kod bloğunu ekleyebilirsiniz.

services.AddControllers().AddNewtonsoftJson(options =>
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);

3. Çözüm

Biraz daha yorucu olsa da ilişkisel modellerinize [JsonIgnore] Attribute’ünü ekleyebilirsiniz.

public class User{ 
public int Id {get;set;}
public int
Username {get;set;}
[JsonIgnore]
public Store Store {get;set;}
}

Örnek Hata Çıktısı

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 32.
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.WriteAsyncCore(Stream utf8Json, Object value, Type inputType, JsonSerializerOptions options, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
 — — End of stack trace from previous location where exception was thrown — -
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
HEADERS
=======
Accept: */*
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 73
Content-Type: application/json
Host: localhost:44302
User-Agent: PostmanRuntime/7.26.2
Postman-Token: c68d87a6-bf4f-4fe5–912d-aa9646756960
  • *Bu yazı 30.07.2020 tarihinde yazılmıştır. Daha sonrasındaki gelecek sürümlerle değişebilir.

You may also like...