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

System.PlatformNotSupportedException with UWP and .Net Native #269

Closed
gr001 opened this issue Nov 6, 2019 · 29 comments
Closed

System.PlatformNotSupportedException with UWP and .Net Native #269

gr001 opened this issue Nov 6, 2019 · 29 comments
Assignees
Labels
bug (cc: fix) Something isn't working
Milestone

Comments

@gr001
Copy link

gr001 commented Nov 6, 2019

Hello,
I would like to use the serializer in a UWP application but when I compile with .Net Native turned on I get "System.PlatformNotSupportedException: 'Dynamic code generation is not supported on this platform.'". Do you think it could be fixed or it means major changes in the serializer. I also attached a sample project. Just hit the 'Test' button.
TestSerializer.zip

This is the callstack that I am getting:

at System.Reflection.Emit.ReflectionEmitThrower.ThrowPlatformNotSupportedException() in f:\dd\ndp\fxcore\CoreRT\src\System.Private.CoreLib\src\System\Reflection\Emit\ReflectionEmitThrower.cs:line 11
at System.Reflection.Emit.DynamicMethod..ctor(String name, Type returnType, Type[] parameterTypes, Module m, Boolean skipVisibility) in f:\dd\ndp\fxcore\CoreRT\src\System.Private.CoreLib\src\System\Reflection\Emit\DynamicMethod.cs:line 38
at ExtendedXmlSerializer.Core.LightInject.ServiceContainer.DynamicMethodSkeleton.CreateDynamicMethod(Type returnType, Type[] parameterTypes) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\Core\LightInject\LightInject.cs:line 4031
at ExtendedXmlSerializer.Core.LightInject.ServiceContainer.DynamicMethodSkeleton..ctor(Type returnType, Type[] parameterTypes) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\Core\LightInject\LightInject.cs:line 4007
at ExtendedXmlSerializer.Core.LightInject.ServiceContainer.<>c.<.ctor>b__22_1(Type returnType, Type[] parameterTypes) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\Core\LightInject\LightInject.cs:line 1903
at System.Func3.Invoke(T1 arg1, T2 arg2) at ExtendedXmlSerializer.Core.LightInject.ServiceContainer.CreateDynamicMethodDelegate(Action1 serviceEmitter) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\Core\LightInject\LightInject.cs:line 3017
at ExtendedXmlSerializer.Core.LightInject.ServiceContainer.CreateDelegate(Type serviceType, String serviceName, Boolean throwError) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\Core\LightInject\LightInject.cs:line 3905
at ExtendedXmlSerializer.Core.LightInject.ServiceContainer.CreateDefaultDelegate(Type serviceType, Boolean throwError) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\Core\LightInject\LightInject.cs:line 3868
at ExtendedXmlSerializer.Core.LightInject.ServiceContainer.GetInstance(Type serviceType) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\Core\LightInject\LightInject.cs:line 2732
at ExtendedXmlSerializer.ExtensionModel.Services.GetService(Type serviceType) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\ExtensionModel\Services.cs:line 184
at ExtendedXmlSerializer.Core.Extensions.Get[T](IServiceProvider this) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\Core\Extensions.cs:line 217
at ExtendedXmlSerializer.Configuration.RootContext.Create() in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\Configuration\RootContext.cs:line 60
at ExtendedXmlSerializer.Configuration.Extensions.Create(IContext this) in C:\projects\extendedxmlserializer\src\ExtendedXmlSerializer\Configuration\Extensions.cs:line 41
at TestSerializer.MainPage.Button_Click(Object sender, RoutedEventArgs e) in C:\Projects\tests\TestSerializer\TestSerializer\MainPage.xaml.cs:line 36

@Mike-E-angelo Mike-E-angelo self-assigned this Nov 6, 2019
@Mike-E-angelo Mike-E-angelo added the bug (cc: fix) Something isn't working label Nov 6, 2019
@Mike-E-angelo
Copy link
Member

Doh, sorry about that @gr001! Looks like this is an issue with LightInject in how it is composing a dynamic method. Indeed, dynamic content is not allowed in UWP, which is one of many many reasons why I stay away from it. 😆

I have opened an issue on LightInject's board. Feel free to follow along here:
seesharper/LightInject#516

@Mike-E-angelo Mike-E-angelo added awaiting reply This issue is waiting for a reply from its author and/or a 3rd party. dependencies Pull requests that update a dependency file labels Nov 8, 2019
@Mike-E-angelo
Copy link
Member

Alright @gr001 I took advantage of our new repo here to update our dependency to LightInject rather than LightInject.Source. That should properly load all its UWP functionality and rule out anything on our side that might be causing it.

You can get the latest version from our new preview feed here:

https://ci.appveyor.com/nuget/extendedxmlserializer-preview

(Please be sure to enable Include prerelease in Nuget explorer.)

@Mike-E-angelo Mike-E-angelo added this to the v2.1.25 milestone Nov 9, 2019
@gr001
Copy link
Author

gr001 commented Nov 9, 2019 via email

@gr001
Copy link
Author

gr001 commented Nov 10, 2019

If you are refering to the version 2.1.24 then I am still getting the same PlatformNotSupportedException with the same callstack. I cannot see any other available version even when I check 'include prerelease' in VS.

@Mike-E-angelo
Copy link
Member

Ugh, you're right @gr001 sorry about that! We've been doing a bunch of housecleaning and catching up with our deployments. Fixing some things and breaking others.

I will look into the nuget, but for now you can download the current v2.1.25.488 nupkg directly here:

https://ci.appveyor.com/api/buildjobs/kh9tnynudj5ecd2o/artifacts/artifacts%2FExtendedXmlSerializer.2.1.25.488-preview.nupkg

If for some reason a new version gets deployed and that one is not available, you can download the latest from here:

https://ci.appveyor.com/project/ExtendedXmlSerializer/extendedxmlserializer/build/artifacts

You should be able to put them in your Visual Studio Offline Packages (or create your own) in the Visual Studio Package sources:

@Mike-E-angelo
Copy link
Member

Looking into this now. There will be further deployments so I am attaching 2.1.25.488-preview to this post.

ExtendedXmlSerializer.2.1.25.488-preview.zip

You can also monitor the following for the latest, as well:

https://ci.appveyor.com/project/ExtendedXmlSerializer/extendedxmlserializer/build/artifacts/

@Mike-E-angelo
Copy link
Member

Boh @gr001 in my case the issue was simple. I had Updates tab selected and not Browse:

Let me know if you still cannot see the package after accounting for the above.

@gr001
Copy link
Author

gr001 commented Nov 13, 2019 via email

@Mike-E-angelo Mike-E-angelo removed the awaiting reply This issue is waiting for a reply from its author and/or a 3rd party. label Nov 13, 2019
@Mike-E-angelo
Copy link
Member

BOOOOOOOOOOOOO. :P I HATE UWPPPPPPPPPPPPPPPPPPPP lololol.

Looks like it's having problems with the metadata token. What does the error message say?

I'm really trying to stay away from having to spend half of my day installing UWP to try to figure this out. :P

@gr001
Copy link
Author

gr001 commented Nov 13, 2019 via email

@Mike-E-angelo
Copy link
Member

What on earth... are you using the latest version of UWP? 😁

A search on that error message turns up nothing. In fact, the only thing I can find is its resource registration:
https://github.com/nguerrera/corefx/blob/34a8a4dd31fb9889b0acbb73bb1a3c8fb22cfd6d/src/System.Reflection.TypeExtensions/src/Resources/Strings.resx#L121

What about the class you are serializing? Are you able to paste it here? I didn't see anything obvious in the zip file.

@Mike-E-angelo
Copy link
Member

Oh, the other recommendation I have is to call EnableMemberExceptionHandling on the ConfigurationContainer. It should tell you the member it is occurring on.

@Mike-E-angelo Mike-E-angelo removed the dependencies Pull requests that update a dependency file label Nov 13, 2019
@gr001
Copy link
Author

gr001 commented Nov 13, 2019 via email

@Mike-E-angelo
Copy link
Member

Ah to be sure, EnableMemberExceptionHandling doesn't hide the error, but rather provides more information about the error -- in particular, the member name that it is occurring on.

Also, I don't seem to see the attachment, unfortunately. Also, feel free to post it as a repo under your account here on GitHub and I can take a look there as well. Repos are preferred as .zip files can contain security threats (unbeknownst to you).

@SuperJMN
Copy link

Does this help in any way?

This is compiling in Release mode over the x64 platform:

image

@Mike-E-angelo
Copy link
Member

Yeah this seems like an UWP-specific issue when building in Release mode. Perhaps there is a build optimization occurring?

Confirming that we are able to run this in Debug mode without any issues.

@Mike-E-angelo
Copy link
Member

Mike-E-angelo commented Nov 28, 2019

Interesting to note that this resource states that a MissingMetadataException is thrown, but in this case we are running into an InvalidOperationException.

https://docs.microsoft.com/en-us/dotnet/framework/net-native/reflection-and-net-native

@Mike-E-angelo
Copy link
Member

OK @gr001 and @SuperJMN check out the following:

#319 (comment)

And see if it fixes your issue.

@Mike-E-angelo Mike-E-angelo modified the milestones: v2.1.25, v2.2.0 Nov 29, 2019
@Mike-E-angelo
Copy link
Member

Mike-E-angelo commented Dec 3, 2019

Alright, this has been deployed to the latest nugetz here:
https://www.nuget.org/packages/ExtendedXmlSerializer/

Further details of the release can be found here:
https://github.com/ExtendedXmlSerializer/home/releases/tag/3.0.0

If you run into any additional problems please open a new issue and we'll take a look into it. Thank you for your contributions towards improving ExtendedXmlSerializer!

An extensible Xml Serializer for .NET that builds on the functionality of the classic XmlSerializer with a powerful and robust extension model.
GitHub
A highly configurable and eXtensible Xml serializer for .NET. - ExtendedXmlSerializer/home

@MattWhilden
Copy link

I'm a little late to the party and this seems like it's been resolved... ? I'm happy to take a look at UWP stuff (I work on the .NET team) if you see more stuff like this. Let me know.

@gr001
Copy link
Author

gr001 commented Dec 3, 2019

I have just upgraded to the latest version and it looks promissing. Thanks a lot for this fix. We rely on this serializer as a replacement of MS serializer which has some crucial limitations when used in UWP.

@MagicAndre1981
Copy link

I'm happy to take a look at UWP stuff (I work on the .NET team) if you see more stuff like this. Let me know.

@MattWhilden why does AppX/UWP not support Assembly.Load() at least for non store releases/sideloading? This raises NotSupportedException.

@Mike-E-angelo
Copy link
Member

I'm a little late to the party

Hey @MattWhilden...

Too obvious? 😁 Thank you for stopping by. We appreciate any and all assistance and guidance you can lend, of course. It is much appreciated!

Thanks a lot for this fix. We rely on this serializer as a replacement of MS serializer which has some crucial limitations when used in UWP.

@gr001 That really means a lot, thank you. I did not know of UWP's difficulties with default .NET functionality but I am not surprised. I am actually amazed anything works at all on that platform. :) Especially ExtendedXmlSerializer since if UWP didn't support MemberInfo.MetadataToken (a NUMBER!) what else is broken?

This raises NotSupportedException

I literally LOL'd on your question @MagicAndre1981... I think @MattWhilden's offer was for ExtendedXmlSerializer-specific problems encountered with UWP. As there are so many gripes, complaints, and identified issues with UWP, giving yourself up for general UWP questions and problems would be a full-time job. 😆

Anyways, all stated, do let us know if there are any further UWP-related ExtendedXmlSerializer issues. Especially now that it seems to do better than the classic XmlSerializer I personally (or rather, my pride) would like to keep it that way. 😇

@MagicAndre1981
Copy link

yes, I know it was offtopic, but this is first time a .NET people was here.

@MattWhilden
Copy link

@MagicAndre1981 Actually had to go digging for the answer to this one. I'm glad I did because the checks were originally introduced in 2011 during the development work for what would become .NET Framework 4.5 for Windows 8 (.NET Core shares a lot with this codebase). The goal was to help better support Windows Store apps (nee Metro at the time) and in particular, deliver on various guarantees around security (you may recall Windows AppContainer being a new hotness feature back then).

The CoreCLR we ship for UWP Debug has this check to this day. Here's the branch we ship it out of if you want to take a look.

My guess is that this was added due to an overabundance of caution but without more analysis it would be hard to tell. In fact, the latest sources for CoreCLR no longer contain any of these checks. I would guess that whatever solution we provide for .NET Core 5 shipping into the Microsoft Store will ship from these more modern sources and not have the check.

Hope that helps.

@Mike-E-angelo
Copy link
Member

Thank you for taking the time to investigate, @MattWhilden. I, like @MagicAndre1981, am honored, a bit surprised, and maybe even a little excited to see someone from the .NET team stop by. 👍

I recently saw the announcement for WinUI 3.0 Alpha. Sounds like they have some serious energy and will behind the initiative, which is super encouraging -- inspiring, even.

Pairing that with the vision of .NET 5 makes me hopeful for the future and that a lot of the historical friction and frustration experienced with UWP will become a thing of the past. 🚀

@MattWhilden
Copy link

My pleasure. Always fun to go crawling through older repositories. Certainly let me know if there's other UWP oddities. I don't always have time to dig in but I often cannot help myself.

@MagicAndre1981
Copy link

In fact, the latest sources for CoreCLR no longer contain any of these checks.

ok, thanks, I see this PR deleted the checks. Hopefully we can get the changed code in UWP at some time.

@MagicAndre1981
Copy link

ok, looks like Microsoft kills now all platform specifics and use same implementation for all platforms:

As of PR #41759, we no longer build the separate implementation for the UAP platform. So, this issue can't occur anymore with .NET 5 because we now will be using the same .NET Core implementation as on Windows, Linux, Mac.

This may also help you @Mike-EEE in future to simplify code even more when .NET 5 is out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug (cc: fix) Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants