Building to iOS with Unity

I’m using the .NET SDK in Unity, all works as expected on Android but iOS is having issues.

If I do the following:

var vehicleBuilder = QueryBuilder<VehicleModel>.New.ContentTypeIs("vehicleModel").Include(3);
var entries = await _client.GetEntries(vehicleBuilder);

I get the error below (I will post just a few lines as it is quite large).

My best guess is it is something to do with Newtonsoft.Json.11.0.2. but can’t confirm for sure.

Any assistance would be great as this is a show stopper (just when I was had it working for Android!)

**NotSupportedException: System.Reflection.Emit.DynamicMethod::.ctor**

**at System.Reflection.Emit.DynamicMethod..ctor (System.String name, System.Type returnType, System.Type[] parameterTypes, System.Type owner, System.Boolean skipVisibility) [0x00000] in <00000000000000000000000000000000>:0** 

**at Newtonsoft.Json.Utilities.DynamicReflectionDelegateFactory.CreateDynamicMethod (System.String name, System.Type returnType, System.Type[] parameterTypes, System.Type owner) [0x00000] in <00000000000000000000000000000000>:0** 

**at Newtonsoft.Json.Utilities.DynamicReflectionDelegateFactory.CreateDefaultConstructor[T] (System.Type type) [0x00000] in <00000000000000000000000000000000>:0** 

**at Newtonsoft.Json.Serialization.DefaultContractResolver.GetDefaultCreator (System.Type createdType) [0x00000] in <00000000000000000000000000000000>:0** 

**at Newtonsoft.Json.Serialization.DefaultContractResolver.InitializeContract (Newtonsoft.Json.Serialization.JsonContract contract) [0x00000] in <00000000000000000000000000000000>:0** 

**at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateArrayContract (System.Type objectType) [0x00000] in <00000000000000000000000000000000>:0** 

**at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract (System.Type objectType) [0x00000] in <00000000000000000000000000000000>:0** 

**at System.Func`2[T,TResult].Invoke (T arg) [0x00000] in <00000000000000000000000000000000>:0** 

**at System.Collections.Concurrent.ConcurrentDictionary`2[TKey,TValue].GetOrAdd (TKey key, System.Func`2[T,TResult] valueFactory) [0x00000] in <00000000000000000000000000000000>:0** 

**at Newtonsoft.Json.Utilities.ThreadSafeStore`2[TKey,TValue].Get (TKey key) [0x00000] in <00000000000000000000000000000000>:0** 

**at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract (System.Type type) [0x00000] in <00000000000000000000000000000000>:0**

Further investigation looks like this is a Newtonsoft.Json issue when attempting to use with iOS.

Is there a way to have a version of the SDK with dependency changed to this version? https://github.com/jilleJr/Newtonsoft.Json-for-Unity

dlls zipped here - https://www.dropbox.com/s/9hxt5muyncaasir/jillejr.newtonsoft.json-for-unity%4012.0.1-patch-001.zip?dl=0

Hi @jk123

This is unfortunate. There’s currently no plans as far as I know of switching the Json.Net dependency in the SDK, but with the upcoming release of https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-apis/ it is very possible that the SDK will shift over to this once it’s generally available.

Best
Robert

Hi @robert

Is it possible to recompile the source code of the SDK to replace the Newtonsoft.Json.dll with the one in the Newtonsoft.Json AOT folder in the above zip?

I’m not looking to move away from Json.NET, just use a modified version for Unity.

Thanks.

Hi @robert

I downloaded the source code and change the nuget Json.NET dependancy to this one:
https://github.com/Daddoon/Json.NET.Aot

I compiled a new contentful .dll and it work for Android, but gave a different error in iOS.

**JsonSerializationException: Unable to find a constructor to use for type Contentful.Core.Models.SystemProperties. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'items[0].sys.space', line 11, position 16.**

Any thought on what this error could be?

Thanks.

I’ve been able to get past this issue by removing the Contentful .dll and replacing it with the source code.

Problems still persist in iOS with an EXC_BAD_ACCESS error so tracking that down now.

I think I’ve got the issue down to this. The dynamic keyword can’t be used in iOS.

It is described here - https://www.wintellect.com/cant-use-dynamic-c-keyword-xamarin-ios/

@robert any thoughts on how to work around this?

In my case I worked around this by changing the SyncedAsset’s Fields type from dynamic to Object and then scripted to handle this change in my code.

This, along with changing the Json library and added a link.xml condition in Unity look to have solve my issue.

That is great @jk123! Happy to hear that you could move forward.

Same Error:

JsonSerializationException: Unable to find a constructor to use for type Contentful.Core.Models.SystemProperties. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path ‘items[0].sys.space’, line 11, position 16.

at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonObjectContract objectContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, Newtonsoft.Json.Serialization.JsonProperty containerProperty, System.String id, System.Boolean& createdFromNonDefaultCreator) [0x00000] in <00000000000000000000000000000000>:0

at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x00000] in <00000000000000000000000000000000>:0

I am using the contentful.Core.dll from the .net sdk and the Newtonsoft.Json.dll nuget
Tested this combination in Unity 2020.1.4f1 and it worked before, now it won’t.
The only thing that changed is the iOS version (updated from 12.x to 13.x)
Could this really be the issue?

@jk123 seemed like we ran into the same error. Could you provide some extra details on how you fixed this?

I think I have the same issue as described here.

I implemented the Contentful.Core.dll into a Unity 2020.1 project and built for iOS.
In editor everything works fine. The build however throws an error. Could this also be due to an iOS update?
I am using the Newtonsoft.Json.dll nuget.

JsonSerializationException: Unable to find a constructor to use for type Contentful.Core.Models.SystemProperties. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path ‘items[0].sys.space’, line 11, position 16.

at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject (Newtonsoft.Json.JsonReader reader,

@jk123, Do you have some extra information on how you managed to fix this?

@robert1 Try removing the Contentful dll and using the source code instead.

This did the trick, thanks for the quick response!

1 Like

Hello @jk123 , @robert1 , could you guys perhaps elaborate a bit on how you got this working?
I’m trying to get Contentful to work with Unity3D 2020.1.16, but so far I’m struggling.
I’m pretty new to both Unity and .NET/C#, so perhaps it’s less of an issue than it seems to me for the moment.
After reading this thread I downloaded the https://github.com/contentful/contentful.net code as a zip and placed the contentful.core folder in to my Assets/plugins folder, accompanied by the Newtonsoft.Json.dll found on Nuget.
Currently I’m getting the following error:

Assets/Plugins/Contentful.Core/Configuration/ExtensionJsonConverter.cs(53,102): error CS0656: Missing compiler required member ‘Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create’

Apart from that I can’t seem to reference the Contentful referenced in other files through

Using Contentful.Core;

Am I going about this completely going with the source files? Do I manually have to add a ProjectReference somewhere?

Kind regards,
aSt