-2

I have created an API that is returning an object A, containing object B and object B is containing object A, see example below:

public class Person() { 
    string Name {get;set;}
    List<Schema> Schemas {get;set;}
}
public class Schema(){
    List<Person> Persons {get;set;}
}

In C# this will not be a problem, and an exception will not be catched (I've added try-catch-statements everywhere.

Problem: When I tried out the function using swagger the exception thrown to the client is 500.

The client calling the API is telling me that the it's a cors policy validation. This I know for sure that this is not the problem since it works well in other scenarios.

Antfolkarn
  • 101
  • 5
  • 4
    In Stack Overflow, it's OK to answer your own question, but it would be more appropriate to do so by formulating the question in the question section, then adding your own answer. The question also needs a few more details about the context in which the error occurred to make the question/answer helpful to others (which is what SO is about) – Christopher Hamkins Oct 22 '21 at 13:00
  • It would be preferable for you to find the most popular/best written (possibly unanswered) question that is directly attributable to this, write your words as an answer, then maybe also vote to close all the other questions that are the same thing, as a duplicate of the one you wrote the perfect answer in; that would be a great contribution to the site, get you upvotes for your answer and close out some of the noise, as well as help establish a canonical question on the topic. Probably comes up reasonably often; I see it when people ser object graphs created by Enitity Framework(bidirectional) – Caius Jard Oct 22 '21 at 13:28
  • Thank you for your comments! @ChristopherHamkins - I've separated the question and answer and added more detailed information. – Antfolkarn Oct 25 '21 at 07:45
  • 1
    @CaiusJard - I've found about 3 other posts similiar to this, but it has been using other frameworks. In .Net 2 - 3 there was another working solution for this using Newtonsoft so I did not want to put my solution together with an already working solution. – Antfolkarn Oct 25 '21 at 07:45
  • Yeah, typically it's caused by serializing your database entities, which isn't ideal. All in, problem goes away by switching to a structure that separates db ents from presentation ones, and the presentation ones don't have cycles in – Caius Jard Oct 25 '21 at 08:48

1 Answers1

0

To view the actual problem I had to go to windows "Event Logger".

From event logger i found this:

Exception: 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. at System.Text.Json.ThrowHelper.ThrowJsonException_SerializerCycleDetected(Int32 maxDepth) at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)

To solve the problem with circular reference I added the code below in the configuration (startup.cs, API):

   services.AddMvc()
                .AddJsonOptions(opt =>
                 {
                     opt.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.Preserve;
                 });

Documentation can be found at: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to?pivots=dotnet-5-0

Antfolkarn
  • 101
  • 5