applejag/Newtonsoft.Json-for-Unity

Bug: System.MissingMethodException: Constructor on type 'System.ComponentModel.ReferenceConverter' not found

MartinHaeusler opened this issue ยท 4 comments

Hi!

First of all, thank you very much for providing this Unity-specific branch of newtonsoft.json!

I'm trying to get a relatively complex domain model (lots of nesting, lists, polymorphism...) to serialize to / deserialize from JSON in a Unity WebGL build. As per usual for those kinds of problems, it's working perfectly fine within the editor and as a Windows Mono Build, but produces the following exception within a WebGL build:

System.MissingMethodException: Constructor on type 'System.ComponentModel.ReferenceConverter' not found.
 at System.RuntimeType.CreateInstanceImpl(System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes, System.Threading.StackCrawlMark& stackMark) [0x00000]

Apparently, we are not alone with this issue and it seems to be related to the AOT compiler throwing stuff out which ends up being needed. In this particular case, I have no idea why this particular class is needed at runtime, I don't use anything from System.ComponentModel in my entire project.

As I mentioned, the domain model I'm working with has its complexities. I therefore use JsonSubTypes for deserialization of polymorphic objects and fields based on a jsonTypeName property.

As I currently don't care about bundle size what-so-ever (I just want to get this up and running, really), my link.xml (placed under /Assets/Code) looks like this:

<linker>
    <assembly fullname="Newtonsoft.Json" preserve="all"/>
    <assembly fullname="JsonSubtypes" preserve="all"/>
</linker>

Is there anything wrong with my setup? Any advice on the matter would be highly appreciated, I'm hitting walls left and right with this...

Details

Host machine OS running Unity Editor ๐Ÿ‘‰ Windows 10

Unity build target ๐Ÿ‘‰ WebGL

Newtonsoft.Json-for-Unity package version ๐Ÿ‘‰12.0.201

I was using Unity version ๐Ÿ‘‰ 2020.2.6f1

Checklist

  • Shutdown Unity, deleted the /Library folder, opened project again in Unity, and problem still remains.
  • Checked to be using latest version of the package.

Ok, after a lot of digging I managed to resolve this one myself. Here's the key comment (it was regarding Xamarin, but it's pretty much the same deal):

xamarin/xamarin-macios#3144 (comment)

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.

To solve the issue, the link.xml has to contain the following entry:

        <assembly fullname="System">
                <type fullname="System.ComponentModel.ReferenceConverter">
                        <method signature="System.Void .ctor(System.Type)" />
                </type>
        </assembly>

Hi @MartinHaeusler, great debugging! I know myself that these AOT issues are not that fun to track down.

In the latest version of this package, v12.0.302, I actually managed to embed a link.xml file into the package. In there I've added preservations for any System.ComponentModel converters, as they're quite commonly used with Json .NET.

https://github.com/jilleJr/Newtonsoft.Json-for-Unity/blob/40ba6b123df0a2702c7e26f1230271c0752f3fd4/Src/Newtonsoft.Json/Resources/link.xml#L4

So if you would update the package you have installed to the latest, v12.0.302, then that should also resolve the issue.

Peace โœŒ๏ธ

Hi @jilleJr ! Sorry for the late reply, busy times.

I just removed the "System" entry from my link.xml and upgraded to v12.0.302 instead. I can confirm that your latest patch indeed got rid of the issue entirely. I'm happy that you support this library also when it comes to issues such as this, because I actually have a JVM background and I have no idea about the internals of the CLR, which is why I needed a lot of digging through google results to come up with the solution above.

@MartinHaeusler hehe well so far you've surely proven yourself capable of researching.

I'm up for answering any questions you got. If you feel that the issues are not the right place, check out the discussions: https://github.com/jilleJr/Newtonsoft.Json-for-Unity/discussions
It's completely unused, except some issues I've transferred there, but feel welcome in using it all you can :)