Xamarin-macios: [Newtonsoft.Json] Constructor on type System.ComponentModel.ReferenceConverter not found.

Created on 1 Jan 2018  路  10Comments  路  Source: xamarin/xamarin-macios

Copied from https://github.com/JamesNK/Newtonsoft.Json/issues/1472 @bunkerbilly


Xamarin
IOS 9.3

Hi,

When i run JsonConvert.DeserializeObject<[Customer Class]> i get the Exception

"Constructor on type 'System.ComponentModel.ReferenceConverter' not found."

Any ideas on what i can do to solve this?

iOS question

Most helpful comment

Hello Again!

@arielbh helped us to figure this out with his project (thanks a lot!). The issue is that something is trying to instantiate via Activator class an instance of System.ComponentModel.ReferenceConverter via its System.Type ctor but since it isn't referenced directly in you application it gets removed by the managed linker. In such cases the linker needs some help by providing a custom linker configuration file so you can instruct the linker to not remove said type ctor, you do this by creating a xml file (Linker.xml in this case) and add it to your Xamarin.iOS project.

Linker.xml contents:

<?xml version="1.0" encoding="UTF-8" ?>
<linker>
        <assembly fullname="System">
                <type fullname="System.ComponentModel.ReferenceConverter">
                        <method signature="System.Void .ctor(System.Type)" />
                </type>
        </assembly>
</linker>

Make sure Linker.xml's build action is set to LinkDescription [0].

At the time of writing this, Visual Studio on Windows does not have a way to set LinkDescription build action but you can easily do this by adding the following to your .csproj file:

<ItemGroup>
  <LinkDescription Include="Linker.xml" />
</ItemGroup>

Hope this helps!

All 10 comments

Hello @JamesNK

I've been trying to reproduce this issue without luck, basically I came up with the following test case using the material on https://github.com/JamesNK/Newtonsoft.Json/issues/1472

using System;
using Newtonsoft.Json;
using UIKit;

namespace GHIssue3144 {
    public partial class ViewController : UIViewController {
        protected ViewController (IntPtr handle) : base (handle) {
            // Note: this .ctor should not contain any initialization logic.
        }

        public override void ViewDidLoad () {
            base.ViewDidLoad ();

            var obj = new JsonBugTest { AssistantAssociateid = 1 };
            var str = JsonConvert.SerializeObject (obj);
            Console.WriteLine (str);

            var obj2 = JsonConvert.DeserializeObject<JsonBugTest> (str);
            Console.WriteLine (obj2.AssistantAssociateid);
        }
    }

    public class JsonBugTest {

        [JsonProperty ("AssistantAssociateId")]
        public int? AssistantAssociateid { get; set; }
    }
}

Our guess was that the managed linker was removing a needed ctor since mono has the class available but I tried the following configurations on an iPhone 6 Plus with iOS 11.2.1 and it showed no issues.

| Linker | Debug/Release |
| --- | --- |
| Link All | Release |
| Don't Link | Debug |
| SDK Only | Debug |

The testing was done using the latest Xamarin stable release

=== Visual Studio Enterprise 2017 for Mac ===

Version 7.3.2 (build 12)
Installation UUID: 4e014908-906a-4d2b-9aed-1d598a6d71cf
Runtime:
Mono 5.4.1.7 (2017-06/e66d9abbb27) (64-bit)
GTK+ 2.24.23 (Raleigh theme)

Package version: 504010007

=== NuGet ===

Version: 4.3.1.4445

=== .NET Core ===

Runtime: /usr/local/share/dotnet/dotnet
Runtime Version: 2.0.3
SDK: /usr/local/share/dotnet/sdk/2.0.3/Sdks
SDK Version: 2.0.3
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.4.1/lib/mono/msbuild/15.0/bin/Sdks

=== Apple Developer Tools ===

Xcode 9.2 (13772)
Build 9C40b

=== Xamarin.iOS ===

Version: 11.6.1.2 (Visual Studio Enterprise)
Hash: 6857dfcc
Branch: xcode9.2
Build date: 2017-12-04 21:20:41-0500

=== Xamarin.Mac ===

Version: 4.0.0.214 (Visual Studio Enterprise)

=== Build Information ===

Release ID: 703020012
Git revision: 7d0c9d7ad46b125cf4b7bbf9532c0531b6433ce6
Build date: 2017-12-12 14:03:18-05
Xamarin addins: 6dddaa49fbecb2695fcdcd6b712eb269e288518c
Build lane: monodevelop-lion-d15-5

=== Operating System ===

Mac OS X 10.13.2
Darwin 17.3.0 Darwin Kernel Version 17.3.0
Thu Nov 9 18:09:22 PST 2017
root:xnu-4570.31.3~1/RELEASE_X86_64 x86_64

In order to track this down we need a reproducible test case or more detailed directions on how to reproduce this.

Thanks!!

Perhaps this code is being linked out?

@migueldeicaza Yeah, that was our first impression and it is very likely that it is the issue but even with Link All and Release config our small test case does not crash/fail.

We're having a similar issue on our project, I wonder if it might be something that got addressed in a relatively recent Xamarin? For various reasons, my project lags a few versions behind the latest Xamarin libraries.

Here's what's on my dev machine, it's actually newer than our build machine:

=== Visual Studio Professional 2017 for Mac ===

Version 7.2.2 (build 7)
Installation UUID: bed6ebe3-1f09-47c7-8642-5a3cb1186f3a
Runtime:
Mono 5.2.0.224 (d15-3/14f2c81) (64-bit)
GTK+ 2.24.23 (Raleigh theme)

Package version: 502000224

=== NuGet ===

Version: 4.3.1.4445

=== .NET Core ===

Runtime: /usr/local/share/dotnet/dotnet
Runtime Version: 2.0.0
SDK: /usr/local/share/dotnet/sdk/2.0.0/Sdks
SDK Version: 2.0.0
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.2.0/lib/mono/msbuild/15.0/bin/Sdks

=== Xamarin.Profiler ===

Version: 1.5.6
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Apple Developer Tools ===

Xcode 9.0 (13247)
Build 9A235

=== Xamarin.Mac ===

Version: 3.8.1.0 (Visual Studio Professional)

=== Xamarin.Android ===

Version: 8.0.2.1 (Visual Studio Professional)
Android SDK: /Users/owen/Library/Developer/Xamarin/android-sdk-macosx
Supported Android versions:
2.3 (API level 10)
4.0.3 (API level 15)
4.1 (API level 16)
4.4 (API level 19)
5.0 (API level 21)
6.0 (API level 23)
7.0 (API level 24)
7.1 (API level 25)

SDK Tools Version: 25.2.5
SDK Platform Tools Version: 27.0.1
SDK Build Tools Version: 25.0.3

Java SDK: /usr
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Xamarin.iOS ===

Version: 11.2.1.0 (Visual Studio Professional)
Hash: 12e80e01
Branch: d15-4-xi
Build date: 2017-10-13 12:03:42-0400

=== Xamarin Inspector ===

Version: 1.3.2
Hash: 461f09a
Branch: 1.3-release
Build date: Tue, 03 Oct 2017 18:26:57 GMT
Client compatibility: 1

=== Build Information ===

Release ID: 702020007
Git revision: 2236098c68f51eeb447cb947a87371d1e84f6213
Build date: 2017-10-23 11:43:05-04
Xamarin addins: eaa605cf97f25ce184949f32c712b465ca96f665
Build lane: monodevelop-lion-d15-4

=== Operating System ===

Mac OS X 10.12.6
Darwin 16.7.0 Darwin Kernel Version 16.7.0
Thu Jun 15 17:36:27 PDT 2017
root:xnu-3789.70.16~2/RELEASE_X86_64 x86_64

This is "closed"? lol. It's always something with xamarin, anytime something is updated. I'm simply tired of it myself. I have the same problem, and cannot distribute my latest app until this is resolved. I maintain UWP, Android and iOS versions. Combined, there's always issues to contend with. After hanging in for several years, I'm beginning to wonder if this environment will EVER work for robust, complex, rich apps. So far, no. Try upgrading to latest version of vs 2017 for mac and xamarin forms. Have a PCL in place that does the actual serialization, compile an iOS app that references this pcl. Maybe you can find something there. Xamarin: Leaving this issue "as-is" is not acceptable.

Also, try a different device with the latest iOS. I'm testing on an iPad Pro. By the way, this is not an issue with the emulator - only on actual devices.

BTW: I just solved the issue myself. Uninstall the newtonsoft package, and then add it back. Problem solved for me. Still don't like the flippant attitudes around here - some of us have thousands of hours of work tied up in Xamarin apps, as well as clients and livelihoods. Keep that in mind, please.

This is "closed"?

@cmello418 This isn't closed :) but we would really want to get to the bottom of this issue. If you or someone else is able to reproduce this please share either the steps to get into this error condition or a test case. If you are into the position to share your project that has this issue please fill private bug at https://bugzilla.xamarin.com

Hello Again!

@arielbh helped us to figure this out with his project (thanks a lot!). The issue is that something is trying to instantiate via Activator class an instance of System.ComponentModel.ReferenceConverter via its System.Type ctor but since it isn't referenced directly in you application it gets removed by the managed linker. In such cases the linker needs some help by providing a custom linker configuration file so you can instruct the linker to not remove said type ctor, you do this by creating a xml file (Linker.xml in this case) and add it to your Xamarin.iOS project.

Linker.xml contents:

<?xml version="1.0" encoding="UTF-8" ?>
<linker>
        <assembly fullname="System">
                <type fullname="System.ComponentModel.ReferenceConverter">
                        <method signature="System.Void .ctor(System.Type)" />
                </type>
        </assembly>
</linker>

Make sure Linker.xml's build action is set to LinkDescription [0].

At the time of writing this, Visual Studio on Windows does not have a way to set LinkDescription build action but you can easily do this by adding the following to your .csproj file:

<ItemGroup>
  <LinkDescription Include="Linker.xml" />
</ItemGroup>

Hope this helps!

What about updating Xamarin including the metadata for ReferenceConverter by default? This seems like a fairly common issue.

Was this page helpful?
0 / 5 - 0 ratings