runtime: JSON deserialization fails in iOS release build if class implements an interface

Description

A simple JSON deserialization using Newtonsoft.Json fails on iOS in a release build as soon as the class that is trying be be de-serialized implements an interface.

The same app works in Debug build in the iOS simulator as expected.

Reproduction Steps

  1. Create a new iOS app with dotnet new ios

  2. Set a valid bundle identifier that allows installing the app on a physical device

  3. Add this code to the project:

    public class ProductImage : IEntity
    {
       public string ProductNumber { get; set; }
       public Guid Id { get; set; }
    }
      
    public interface IEntity
    {
       Guid Id { get; set; }
    }
    

    During startup, run this code:

    var x = @"{
       ""productNumber"": ""P1"",
       ""id"": ""46c67d7c-fd15-4d89-9fed-991c48c1bacf""
    }";
    
    var pi = JsonConvert.DeserializeObject<ProductImage>(x);
    
    
  4. Build the app for a device: dotnet publish testapp.csproj -f:net6.0-ios -c:Release /p:CodesignKey="Apple Development: ******" /p:CodesignProvision="Test app" /p:ArchiveOnBuild=true -r ios-arm64 --self-contained

  5. Deploy to device and run

Full project file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0-ios</TargetFramework>
    <OutputType>Exe</OutputType>
    <Nullable>enable</Nullable>
    <ImplicitUsings>true</ImplicitUsings>
    <SupportedOSPlatformVersion>13.0</SupportedOSPlatformVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
  </ItemGroup>
</Project>

Self-contained example project: https://github.com/markuspalme/dotnetruntime-75802

Expected behavior

The app starts successfully and manages to de-serialize the JSON regardless of whether the class implements an interface or not.

Note that removing the interface will make the de-serialization work as expected.

Actual behavior

JSON deserialization fails with this exception:

Error setting value to 'Id' on 'testapp.ProductImage'.

Regression?

The same code works in Xamarin.iOS.

Known Workarounds

No response

Configuration

.NET 6.0.401 iOS 16, same on 15.6.1

Other information

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 3
  • Comments: 48 (24 by maintainers)

Most upvoted comments

@markuspalme would you be able to try this tool by @vitek-karas to see if you can obtain a repro? https://github.com/vitek-karas/illinkrepro

You’ll need to clone it on the machine where you ran the publish command and obtained the binlog. Then inside of the cloned directory, do dotnet run -- create path/to/msbuild.binlog. It tries creates a repro directory containing the linker command-line and input assemblies. If you can share that with us, it would be very helpful.