31

I had my working project written in asp.net core 2.1 for a long time, but yesterday, I was forced to upgrade it to .net core 3.0 (due to 2.1 cannot call Dll' s which are written in 3.0 already).

With that, a lot of functions were obsolete or already removed. I fixed almost all of it, but one problem with CORS.

Like many people before me, I used:

app.UseCors(x => x
  .AllowAnyOrigin()
  .AllowAnyMethod()
  .AllowAnyHeader()
  .AllowCredentials());

in Configure function. And services.AddCors() in ConfigureServices function.

I was able to fixed this quite easily with setting WithOrigins() or .SetIsOriginAllowed(_ => true) instead of AllowAnyOrigin() which does not work anymore with AllowCredentials().

After that, I was able to start the application and I thought everything is fine, but then I get stuck until now with problem I do not know, how to fix.

I have DB relation N:N and relation table which handle that, that means I have Admin entity with AdminProject list property, then I have AdminProject entity with Admin list and Project list properties and Project entity with AdminProject list property once again.

When I am listing my projects of certain admin, I am returning in Controller this return Ok(projects), where I just use getAll on AdminProject entity and then with Select return only project.

For that, I have to use[JsonIgnore] in project/admin for properties which I do not need to avoid cycling when creating json.

With that said: NOW IN .NET CORE 3.0 AND CORS SETTINGS IT DOES NOT WORK.

I am getting an error: 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.

when debugging in console and error Access to XMLHttpRequest at 'http://localhost:5000/api/project/adminlist/1' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. in WEB browser

I think I tried almost everything with Cors settings etc and I do not know why is this happening now. I also tried to JsonConvert.SerializeObject() before return it ---> return Ok(JsonConvert.SerializeObject(projects)) and this is working, but I am not able (mentally) to do this in every single controllers functions.

Please help! Thanks a lot!

Nahid Islam
  • 193
  • 1
  • 5
  • 15
Vojtěch Mráz
  • 1,116
  • 1
  • 9
  • 19
  • I'm not sure that this is a CORs problem - I think it could be to do with your object having an internal cyclical relationship. i.e. A has property B which has property A, etc. Why don't you try it with an object you know does not have such a relationship? – SBFrancies Sep 12 '19 at 18:19
  • I was thinking the same, but i have the jsonignore annotations which worked in 2.1 perfectly.. and cyclical relationship is there due to n:n relationship in database. I am joining two tables with another joining table which stores the n:n relation and makes the model cyclical. But thanks, on a one hand, i think it is cors problem, but on the other i rly dont know anymore – Vojtěch Mráz Sep 12 '19 at 20:45
  • 1
    I would try adding `services.AddMvc() .AddJsonOptions( options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore );` in your Startup.cs file to see if that fixes the issue. – SBFrancies Sep 12 '19 at 20:53
  • 1
    Man, I dont even know what to say. This really fixed my issue. Just to be completely clear. I had to `Microsoft.AspNetCore.Mvc.NewtonsoftJson` download this Nuget, because JSOn.NET is not in .net core 3.0 and then use `.AddNewtonsoftJson`. Thank you very much @SBFrancies . – Vojtěch Mráz Sep 12 '19 at 21:11
  • I have the same problem. Could you post a complete answer, please. I'm having trouble pulling together the pieces. Happy to vote it up if it works. – Quark Soup Sep 24 '19 at 16:01
  • @Quarkly I added answer, I hope it will be enough because that is actually all what I did and what helped me. – Vojtěch Mráz Sep 24 '19 at 16:30
  • In reference to @SBFrancies' comment, .NET Core 3 no longer supports `ReferenceLoopHandling`: [https://stackoverflow.com/questions/58006152/net-core-3-not-having-referenceloophandling-in-addjsonoptions](https://stackoverflow.com/questions/58006152/net-core-3-not-having-referenceloophandling-in-addjsonoptions) – codeMonkey Feb 26 '20 at 22:09

2 Answers2

72

The problem was occurring because in .NET Core 3 they change little bit the JSON politics. Json.Net is not longer supported and if you want to used all Json options, you have to download this Nuget: Microsoft.AspNetCore.Mvc.NewtonsoftJson.

After that in your Startup.cs file change/fix/add line where you are adding MVC (in the ConfigureServices method.

So: here is what I did and what fixed my issue:

services.AddMvc(option => option.EnableEndpointRouting = false)
                .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
                .AddNewtonsoftJson(opt => opt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore);

I hope it will help somebody else. Cheers!

Vojtěch Mráz
  • 1,116
  • 1
  • 9
  • 19
  • 11
    Thanks. I found this code did the trick for me: **serviceCollection.AddMvc().AddNewtonsoftJson();** It would appear that the **System.Text.JSON** is Not Ready for Prime Time. – Quark Soup Sep 24 '19 at 16:42
  • This answer fix my issue with using include on EF Core 3.0 and save me some headache. – zAnthony Nov 08 '19 at 21:45
  • This worked but is there any documentation that goes into why this change is required? I wanna know the reasoning. – blankface Jun 10 '20 at 01:57
  • There is a documentation on Microsoft pagfe about System.Text.JSON and how they used NewtonJson etc etc.. If you want more information about this change, try to find it there... – Vojtěch Mráz Jun 10 '20 at 08:18
30

A couple other things have changed in .net core 3 and now instead of using addMVC you can use addControllers. So your code might look like the follow:

services.AddControllers().AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
Brandon.Staley
  • 1,742
  • 21
  • 25
  • Both answers worked for me. Thanks @Brandon.Staley for pointing out that this works with `Mvc` `Controllers` and `ControllersWithViews`, etc. In my case - I was encountering an `object depth exception` with a very simple JSON object being serialized in a Controller Action. It seems __System.Text.JSON__ was an extremely poor decision to implement in this version. – LukeT Nov 07 '19 at 16:14