dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.16k stars 4.72k forks source link

System.Text.Json throw exceptions on UWP in Release Build #29912

Open hez2010 opened 5 years ago

hez2010 commented 5 years ago

Use System.Json.Text in UWP,

JsonSerializer.ToString(new { test = "abc" });
Failure occurred while loading a type.

This operation cannot be carried out because metadata for the following object was removed for performance reasons: <unavailable>
No further information is available. Rebuild in debug mode for better information.
ahsonkhan commented 5 years ago

cc @MichalStrehovsky

MichalStrehovsky commented 5 years ago

You can get more information in the exception message if you switch your build type to Debug and turn on .NET Native in the Debug configuration: switch the project configuration to Debug, and then in the project properties on the Build tab check the checkbox next to "Compile with .NET Native tool chain". The exception message will have more details after you do that.

Reflection-based serializers typically require RD.XML to make them work right out of the box. System.Text.Json wasn't tested on UWP and the experience will be rough.

For UWP, it's recommended to use existing serializers that the .NET Native compiler understands such as DataContractJsonSerializer or Netwonsoft.Json.

(I should probably point out that the reason why the experience it's going to be more rough for System.Text.Json is because this is a framework assembly and a lot of the things in the .NET Native compiler that try to make reflection experience better don't kick in for framework assemblies - framework assemblies are expected to carry their own RD.XML that describes their dynamic behavior so that the compiler can do aggressive treeshaking on them to reduce their size. We didn't make an investment into RD.XML for System.Text.Json.)

hez2010 commented 5 years ago

@MichalStrehovsky I've got detailed exception message:

'System.Text.Json.Serialization.JsonPropertyInfoNotNullable<<>f__AnonymousType2<System.String>,System.String,System.String>' is missing metadata. For more information, please visit http://go.microsoft.com/fwlink/?LinkID=392859

test code:

JsonSerializer.ToString(new { test = "abc" });
MichalStrehovsky commented 5 years ago

Oh, okay, I actually looked and right now it's impossible to make System.Text.Json work on UWP. The JsonPropertyInfoNotNullable type is marked [ReflectionBlocked] in the implementation assembly so .NET Native will completely block it from reflection. No amount of RD.XML can fix that.

When .NET Native support was deleted in CoreFX in dotnet/corefx#38170, along with it we deleted the logic that injects this attribute into framework assemblies. Next System.Text.Json release probably won't have that and we can try again.

mqudsi commented 5 years ago

@MichalStrehovsky

it's impossible to make System.Text.Json work on UWP.

Only if using .NET Native, right? I think the latest version of UWP should run with the regular .NET Core 2.2 runtime otherwise, where STJ seems to be working OK for me.

MichalStrehovsky commented 5 years ago

I think the latest version of UWP should run with the regular .NET Core 2.2 runtime otherwise, where STJ seems to be working OK for me.

.NET Native is still a requirement to ship UWP apps to the Store.

sharwell commented 5 years ago

@ViktorHofer can you clarify the resolution on all these issues that are getting closed?

ViktorHofer commented 5 years ago

@sharwell you asked that question already on another issue. Please see my response there.

This one actually was a mistake and needs to be re-opened.

p2pbsh commented 4 years ago

Just to bump this with something I discovered today, calls to JasonSerializer.Serialize() were working fine using UWP .NET Native against .NET Core 3.0. After updating to .NET Core 3.1 preview 3, calls to Serialize now fail with metadata exceptions.

h82258652 commented 4 years ago

https://github.com/dotnet/runtime/issues/978#issuecomment-567613428 I downgrade to 4.6.0 and it works fine without any rd.xml!

michael-hawker commented 4 years ago

FYI @MattWhilden

Relinking to our Windows Community Toolkit Issue here as well.

MattWhilden commented 4 years ago

Here's the issue I've been using to track this getting resolved. The current design question is around PreserveDependencyAttribute and whether it will be renamed/reused etc. In the mean time we'll need to have rd.xml workarounds.

mc0re commented 4 years ago

I added this to rd.xml, seems to work.

<Namespace Name="System.Text.Json.Serialization.Converters" Browse="Required All"/>
ghost commented 3 years ago

Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

This process is part of the experimental issue cleanup initiative we are currently trialing in a limited number of areas. Please share any feedback you might have in the linked issue.

mqudsi commented 2 years ago

It would be useful if we had some official indication of what the status of UWP is and how it fits into the .NET Core ecosystem in order to figure out if this issue even matters in the grand scheme of things.