3

I have a .NET Core 2.2 Azure functions solution that I'm trying to migrate to .NET ore 3.1. I did the following steps:

  1. I changed the project files target framework to netcoreapp3.1
  2. I updated the Nuget packages to the latest stable version
  3. I updated the global.json to
{
    "sdk": {
        "version": "3.1.100"
    }
}
  1. I have no PackageReference to "Microsoft.AspNetCore.App" in the project files
  2. I installed the SDK from https://dotnet.microsoft.com/download/dotnet-core/3.1
  3. I ran pm install -g azure-functions-core-tools@3

Everything went fine but at runtime, I had a serialization issue:

Newtonsoft.Json: Self-referencing loop detected with type 'Platform'. Path '[0].hierarchy.platform'."

I have the 2 DTOs:

public class Platform
{
    [JsonRequired]
    public string PlatformId { get; set; }

    [JsonRequired]
    public Guid HierarchyUuid { get; set; }

    public Hierarchy Hierarchy { get; set; }
}

public class Hierarchy
{
    [JsonRequired]
    public string HierarchyId { get; set; }

    [JsonIgnore]
    public IEnumerable<Platform> Platform { get; set; }
}

I understand the reason of the issue but with .Net Core 2.2 as target framework this DTOs were successfully serialized. As mentioned by Microsoft, in the 3.1 version the serialization has been changed and I guess this is the cause of the issue. I replaced Newtonsoft.Json by System.Text.Json but still had the same issue. Finally, to prevent the circular references between Platform and Hierarchy I kept Newtonsoft.Json and added in the FunctionStartup.Configure method the following:

builder.Services.AddMvcCore().AddNewtonsoftJson(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);

Which needs the Microsoft.AspNetCore.Mvc.NewtonsoftJson package.

When I installed the package version 3.1.1 I got a build error

Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'Microsoft.AspNetCore.Mvc.Core, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

Does anybody have an idea why I have this error and how to resolve the issue?

TylerH
  • 20,799
  • 66
  • 75
  • 101
teost
  • 31
  • 2
  • 1
    You should make sure you're using the correct `JsonIgnoreAttribute`. Both [System.Text.Json](https://learn.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonignoreattribute?view=netcore-3.1) and [Newtonsoft](https://www.newtonsoft.com/jsonschema/help/html/T_Newtonsoft_Json_JsonIgnoreAttribute.htm) have an attribute of this name. – dbc Jan 23 '20 at 17:45
  • @teost did you manage to solve it? Could you please describe. – Andriy Shevchenko May 05 '21 at 18:51

0 Answers0