question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Android .net6 project throw exception when serilaize Tuple in Release mode (v13.0.2)

See original GitHub issue

Steps

  1. Need create Android project on .net6
  2. Add reference to Newtonsoft.Json lib 13.0.2
  3. Create simple class with Tuple and try to serialize it.

Result: Debug mode works well. Release mode will throw exception.

Code simple

var tuple = new Tuple<decimal, string>(1, "one");
var json = JsonConvert.SerializeObject(tuple, Formatting.Indented);

Actual behavior

Code in release mode will trow exception after try serialize

Newtonsoft.Json.JsonSerializationException
Newtonsoft.Json.JsonSerializationException: A member with the name '' already exists on 'System.Tuple`2[System.Decimal,System.String]'. Use the JsonPropertyAttribute to specify another name.

Newtonsoft.Json.Serialization.JsonPropertyCollection.AddProperty(JsonProperty property)
Newtonsoft.Json.Serialization.DefaultContractResolver.CreateConstructorParameters(ConstructorInfo constructor, JsonPropertyCollection memberProperties)
Newtonsoft.Json.Serialization.DefaultContractResolver.CreateObjectContract(Type objectType)
Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract(Type objectType)
System.Collections.Concurrent.ConcurrentDictionary`2[[System.Type, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[Newtonsoft.Json.Serialization.JsonContract, Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed]].GetOrAdd(Type , Func`2 )
Newtonsoft.Json.Utilities.ThreadSafeStore`2[[System.Type, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[Newtonsoft.Json.Serialization.JsonContract, Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed]].Get(Type key)
Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract(Type type)
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.WriteStartArray(JsonWriter writer, Object values, JsonArrayContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer)
Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Type type, Formatting formatting, JsonSerializerSettings settings)
Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Formatting formatting, JsonSerializerSettings settings)

Expected behavior

Code will serialize in release mode without exception

Issue Analytics

  • State:open
  • Created 8 months ago
  • Reactions:3
  • Comments:5

github_iconTop GitHub Comments

1reaction
SPuVcommented, Jan 20, 2023

I don’t think switching to System.Text.Json is the best solution. I prefer to use Newtonsoft.Json 😃

At current moment I’ve fixed problem by adding System.Private.CoreLib lib to ignore list for trimming to .proj file

  <Target Name="ConfigureTrimming" BeforeTargets="PrepareForILLink">
    <ItemGroup>
      <ManagedAssemblyToLink Condition="'%(Filename)' == 'System.Private.CoreLib'">
        <IsTrimmable>false</IsTrimmable>
      </ManagedAssemblyToLink>
    </ItemGroup>
  </Target>
0reactions
michaelhannescommented, May 24, 2023

Just my 5 cents, as a newish .net programmer, but lifelong programmer. I am having the same problem with a WASM client, but only when deployed to Azure Webapp.

When running locally there is no problem with retrieving a tuple that has an empty string as a parameter ( boolean, string=“”) - but it will throw exception only in release mode, eg. deployed as web app.

I guess my main contribution to this discussion is that it occurred in a WASM app, and not just android.

it also did not occur under .net 5 when we deployed to Azure Web App - only after migrating to .net 6 & .net 7

api Return -> Item1=true, Item2=“”

failing code: var result = await _httpClient.GetFromJsonAsync<Tuple<bool, string>>(apiPath)

Result I then tried to use Newtonsoft, and System.Text.Json to deserialize json, using _httpClient.GetStringAsync(api_path),

but it always gave a problem with the empty string after publishing to azure web app. either with nullparameter, or nullname, or something like that. i can resetup environment if needed.

hope that helps

Read more comments on GitHub >

github_iconTop Results From Across the Web

Xamarin Android app crash in Release mode (Parse. ...
This exception occur because of network issue not because of debug/release build. In debug mode INTERNET permission is automatically added ...
Read more >
SpanJson 4.0.1
Support for annotating a IDictionary<string,object> with [JsonExtensionData] . Serialization will write all values from the dictionary as additional attributes.
Read more >
CVE-2023-36873– .NET Framework Spoofing Vulnerability
. An unhandled exception has occurred while executing the request. Microsoft.AspNetCore.Http.BadHttpRequestException: Invalid antiforgery token found when ...
Read more >
MessagePack: It's like JSON. but fast and small.
MessagePack is an efficient binary serialization format. It lets you exchange data among multiple languages like JSON. But it's faster and smaller.
Read more >
C# 10 and .NET 6 – Modern Cross-Platform Development
Throwing overflow exceptions with the checked statement ... Chapter 13: Introducing Practical Applications of C# and .NET ... Android: API 21, or later....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found