Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xamarin Android - JsonSerializationException: Error getting value from '[FIELD]' on '[METHOD]' #1113

Closed
JamesNK opened this issue Dec 15, 2017 · 3 comments
Assignees
Labels
Area: App+Library Build Issues when building Library projects or Application projects.

Comments

@JamesNK
Copy link

JamesNK commented Dec 15, 2017

Copied from JamesNK/Newtonsoft.Json#1537 @lorisgiglio


Environment

Visual Studio 2015
Xamarin 4.8.0.754
Xamarin Android 8.1.0.13
Xamarin Android SDK 8.1.1.1
Newtonsoft.Json 10.0.3

Source/destination types

    public class c1
    {
        public string field1 { get; set; }
    }

Expected behavior

It has to serialize the simple object

Actual behavior

12-13 10:49:28.412 9122 9122 I MonoDroid: UNHANDLED EXCEPTION:
12-13 10:49:28.419 9122 9122 I MonoDroid: Newtonsoft.Json.JsonSerializationException: Error getting value from 'field1' on 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'. ---> System.MissingMethodException: Method 'System.Reflection.Emit.DynamicMethod.set_ProfileAPICheck' not found.
12-13 10:49:28.419 9122 9122 I MonoDroid: at System.Linq.Expressions.Compiler.LambdaCompiler.Compile (System.Linq.Expressions.LambdaExpression lambda, System.Runtime.CompilerServices.DebugInfoGenerator debugInfoGenerator) [0x0000f] in :0
12-13 10:49:28.419 9122 9122 I MonoDroid: at System.Linq.Expressions.LambdaExpression.Compile () [0x00000] in :0
12-13 10:49:28.419 9122 9122 I MonoDroid: at Newtonsoft.Json.Utilities.ExpressionReflectionDelegateFactory.CreateGet[T] (System.Reflection.PropertyInfo propertyInfo) [0x0007c] in :0
12-13 10:49:28.419 9122 9122 I MonoDroid: at Newtonsoft.Json.Utilities.ReflectionDelegateFactory.CreateGet[T] (System.Reflection.MemberInfo memberInfo) [0x0000a] in :0
12-13 10:49:28.419 9122 9122 I MonoDroid: at Newtonsoft.Json.Serialization.ExpressionValueProvider.GetValue (System.Object target) [0x0000e] in :0
12-13 10:49:28.419 9122 9122 I MonoDroid: --- End of inner exception stack trace ---
12-13 10:49:28.419 9122 9122 I MonoDroid: at Newtonsoft.Json.Serialization.ExpressionValueProvider.GetValue (System.Object target) [0x00054] in :0
12-13 10:49:28.419 9122 9122 I MonoDroid: at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues (Newtonsoft.Json.JsonWriter writer, System.Object value, Newtonsoft.Json.Serialization.JsonContainerContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonProperty property, Newtonsoft.Json.Serialization.JsonContract& memberContract, System.Object& memberValue) [0x00066] in :0
12-13 10:49:28.419 9122 9122 I MonoDroid: at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject (Newtonsoft.Json.JsonWriter writer, System.Object value, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract collectionContract, Newtonsoft.Json.Serialization.JsonProperty containerProperty) [0x0008d] in :0
12-13 10:49:28.419 9122 9122 I MonoDroid: at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue (Newtonsoft.Json.JsonWriter writer, System.Object value, Newtonsoft.Json.Serialization.JsonContract valueContract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerProperty) [0x000b0] in :0
12-13 10:49:28.419 9122 9122 I MonoDroid: at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value, System.Type objectType) [0x00079] in :0
12-13 10:49:28.419 9122 9122 I MonoDroid: at Newtonsoft.Json.JsonSerializer.SerializeInternal (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value, System.Type objectType) [0x00253] in :0
12-13 10:49:28.419 9122 9122 I MonoDroid: at Newtonsoft.Json.JsonSerializer.Serialize (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value, System.Type objectType) [0x00000] in :0
12-13 10:49:28.419 9122 9122 I MonoDroid: at Newtonsoft.Json.JsonConvert.SerializeObjectInternal (System.Object value, System.Type type, Newtonsoft.Json.JsonSerializer jsonSerializer) [0x00028] in :0
12-13 10:49:28.419 9122 9122 I MonoDroid: at Newtonsoft.Json.JsonConvert.SerializeObject (System.Object value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) [0x00007] in :0
12-13 10:49:28.419 9122 9122 I MonoDroid: at Newtonsoft.Json.JsonConvert.SerializeObject (System.Object value, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in :0

Steps to reproduce

    public static string GetData(string s1)
    {
       if (string.IsNullOrWhiteSpace(s1)) return null;

        c1 c = new c1();

        c1.field1 = s1;

        string res = JsonConvert.SerializeObject(c1);
        
        return res;
   }
@jonpryor jonpryor added the Area: App+Library Build Issues when building Library projects or Application projects. label Dec 15, 2017
@radekdoulik
Copy link
Contributor

I am unable to reproduce it. Tried it with XA Version: 8.1.0.22

If it still happens for you, please attach whole XA project, which reproduces the issue.

@lorisgiglio
Copy link

Hi, it's a big xamarin project with many references and I can't attach all the project.
I've same problem on Android 5.1 and Android 7.1 devices.

The problems seems to depend by dynamic method creation during NewtonSoft serialization but I don't know how it happens and why.

It happens in Release mode and not in Debug mode compilation. I saw that loaded assembly in debug mode are :

Loaded assembly: Microsoft.CSharp.dll [External]
Loaded assembly: System.ComponentModel.TypeConverter.dll [External]
Loaded assembly: Anonymously Hosted DynamicMethods Assembly [External]
Loaded assembly: System.Reflection.Primitives.dll [External]
Loaded assembly: System.Net.Http.dll [External]

My project can't compile in Release mode with Linking "SDK Assembly Only" but only with "NONE".

I attach all compile detailed debug information if it can help.
DEBUG_MODE.txt
RELEASE_MODE_LINKING_NONE.txt
RELEASE_MODE_LINKING_SDKASSEMBLYONLY.txt

@jonpryor
Copy link
Member

From RELEASE_MODE_LINKING_SDKASSEMBLYONLY.txt

error MSB4018: errore imprevisto dell'attivit‡ "LinkAssemblies".
error MSB4018: System.NullReferenceException: Riferimento a un oggetto non impostato su un'istanza di oggetto.
error MSB4018:    in Mono.Linker.TypeReferenceExtensions.<GetMethods>d__4.MoveNext()
error MSB4018:    in Mono.Linker.Steps.TypeMapStep.TryMatchMethod(TypeReference type, MethodDefinition method)
error MSB4018:    in Mono.Linker.Steps.TypeMapStep.GetBaseMethodInTypeHierarchy(TypeDefinition type, MethodDefinition method)
error MSB4018:    in Mono.Linker.Steps.TypeMapStep.MapVirtualMethods(TypeDefinition type)
error MSB4018:    in Mono.Linker.Steps.TypeMapStep.MapType(TypeDefinition type)
error MSB4018:    in Mono.Linker.Steps.TypeMapStep.ProcessAssembly(AssemblyDefinition assembly)
error MSB4018:    in Mono.Linker.Steps.BaseStep.Process(LinkContext context)
error MSB4018:    in Mono.Linker.Pipeline.Process(LinkContext context)
error MSB4018:    in MonoDroid.Tuner.Linker.Process(LinkerOptions options, LinkContext& context)
error MSB4018:    in Xamarin.Android.Tasks.LinkAssemblies.Execute(DirectoryAssemblyResolver res)
error MSB4018:    in Xamarin.Android.Tasks.LinkAssemblies.Execute()
error MSB4018:    in Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
error MSB4018:    in Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext()

I suspect that you're hitting xamarin/xamarin-macios#3215, the fix for which involves a null check within TypeReferenceExtensions.GetMethods(), so I think that this is fixed in:

The fix has not yet been integrated on xamarin-android/master; the last URL above is on xamarin-android/d15-6, which is for Xamarin.Android 8.2 (currently in beta).

However, I don't know when that fix will actually be released.

jonpryor added a commit to jonpryor/xamarin-android that referenced this issue Sep 14, 2020
jonpryor added a commit to jonpryor/xamarin-android that referenced this issue Sep 14, 2020
jonpryor added a commit that referenced this issue Sep 14, 2020
@xamarin xamarin locked as resolved and limited conversation to collaborators Jun 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Area: App+Library Build Issues when building Library projects or Application projects.
Projects
None yet
Development

No branches or pull requests

4 participants