mattjohnsonpint / TimeZoneNames

Provides a complete source of localized time zone names and abbreviations.
MIT License
196 stars 32 forks source link

Crashes on UWP when .NET Native compiled #92

Closed andrewleader closed 1 year ago

andrewleader commented 1 year ago

After updating to 6.0.0 (I think it started with 6.0.0), I get the following exception when calling TZNames.GetDisplayNames() when my app is compiled with .NET Native (works fine in debug without .NET Native).

TimeZoneData.Load ()
System.Reflection.MissingMetadataException: This operation cannot be carried out because metadata for the following object was removed for performance reasons:\n\n EETypeRva:0x00003130\n\nNo further information is available. Rebuild in debug mode for better information.

This seems to be because TimeZoneNames is now using System.Text.Json, which seems to have problems with UWP .NET Native currently: https://github.com/dotnet/runtime/issues/978

Workaround

As described in https://github.com/dotnet/runtime/issues/978#issuecomment-567607904, in your UWP app's Properties/Default.rd.xml file, add the following namespace line for System.Text.Json.Serialization.Converters

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
  <Application>

    <Assembly Name="*Application*" Dynamic="Required All" />

    <!-- Add your application specific runtime directives here. -->

    <!-- Fix System.Text.Json 4.7.0 getting compiled out: https://github.com/dotnet/runtime/issues/978-->
    <Namespace Name="System.Text.Json.Serialization.Converters" Dynamic="Required All"/>

  </Application>
</Directives>

Hope that helps some folks with the same issue! I think it could be possible for TimeZoneNames to include this declarative on behalf of System.Text.Json, but the right fix would be for Microsoft to fix that in the Json package itself. So I'm mostly posting this just as info for other users, feel free to close this if you'd like!

mattjohnsonpint commented 1 year ago

Yeah, I've seen such things before with other projects. I don't publish a UWP-specific target though, so I don't think there's much I can do in this library. Thanks for sharing a workaround though! 👍

mattjohnsonpint commented 1 year ago

FYI - the other place I saw this was https://github.com/getsentry/sentry-dotnet/issues/2029, and actually I believe it also may be due to STJ. Thanks!