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.

PublishTrimmed trims too much (Could not load type 'System.Text.Json.JsonSerializer')

See original GitHub issue

Steps to reproduce

Expected behavior

The program starts successfully.

Actual behavior

grafik

Note that this window only appears if you run the binary from a console - if I doubleclick it in an explorer window, the process exits without displaying that error window. Is that intended or a seperate bug? Shouldn’t it be the other way round?

Environment data

dotnet --info output:

PS C:\Users\Benni\repositories\VncMatrix\VncMatrix> dotnet --info                                                                                                                                                                                                               .NET Core SDK (gemäß "global.json"):                                                                                                                                                                                                                                             Version:   3.0.100
 Commit:    04339c3a26

Laufzeitumgebung:
 OS Name:     Windows
 OS Version:  10.0.18362
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\3.0.100\

Host (useful for support):
  Version: 3.0.0
  Commit:  7d57652f33

.NET Core SDKs installed:
  2.1.505 [C:\Program Files\dotnet\sdk]
  2.1.801 [C:\Program Files\dotnet\sdk]
  2.2.105 [C:\Program Files\dotnet\sdk]
  3.0.100-preview9-014004 [C:\Program Files\dotnet\sdk]
  3.0.100-rc1-014190 [C:\Program Files\dotnet\sdk]
  3.0.100 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0-preview9.19424.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0-rc1.19457.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0-preview9-19423-09 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0-rc1-19456-20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.0.0-preview9-19423-09 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.0.0-rc1-19456-20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
Kinematicscommented, Nov 2, 2019

I’m having basically the same problem, but with the issue that I have no idea how to find what roots I need to include, since the failure is deep inside private WPF code.

The top of a ~90 line stack trace is:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.TypeInitializationException: The type initializer for 'MS.Internal.SystemXmlLinqExtension' threw an exception.
 ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at MS.Internal.SystemXmlLinqExtension..cctor()
   --- End of inner exception stack trace ---
   at MS.Internal.SystemXmlLinqExtension..ctor()
   --- End of inner exception stack trace ---
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean wrapExceptions, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& hasNoDefaultCtor)
   at System.RuntimeType.CreateInstanceDefaultCtorSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean fillCache)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, Boolean wrapExceptions)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
   at System.Activator.CreateInstance(Type type)
   at MS.Internal.AssemblyHelper.LoadExtensionFor(String name)
   at MS.Internal.AssemblyHelper.ExtensionsForSystemXmlLinq(Boolean force)
   at MS.Internal.SystemXmlLinqHelper.IsXElement(Object item)

This is also an open issue in the Mono tracker (https://github.com/mono/linker/issues/712), which directed me at adding:

  <ItemGroup>
    <TrimmerRootAssembly Include="System" />
    <TrimmerRootAssembly Include="PresentationFramework-SystemXmlLinq" />
  </ItemGroup>

However that has no effect. The untrimmed version of the PresentationFramework-SystemXmlLinq.dll file is 18 KB, while the trimmed version (with or without the above directive) is 6 KB. Using dnSpy to look at the class constructor only sees GetType() reflection as anything likely to cause this problem, but making sure System.dll was included didn’t help.

Everything works fine with PublishTrimmed set to false.

Overall, between this and other bugs I’ve encountered, PublishTrimmed has been quite a headache.

0reactions
rewbotcommented, Jun 10, 2020

I’m running into a similar issue with a .NET Core 3.1 single file .exe that is using the PublishTrimmed option. When running it on a Windows 2008 R2 server (6.1.7601) I get the following error:

The type initializer for 'MS.Internal.SystemXmlLinqExtension' threw an exception.

The error does not occur when running the .exe locally on Windows 10 (Version 1909, OS build 18363.900)

Read more comments on GitHub >

github_iconTop Results From Across the Web

PublishTrimmed error with System.Text.Json
I am trying to publish my c# code with the argument -p:PublishTrimmed=true . It uses System.Text.Json, and gives me a warning.
Read more >
Could not load type 'System.Text.Json.Serialization. ...
Create Blazor Web Application from Template. ASP NET 3.0 and run it - ALWAYS Json.Serialization error … Error Could not load type 'System....
Read more >
Migrate from Newtonsoft.Json to System.Text.Json - .NET
Json features, migration will not be possible without significant changes. Allow or write numbers in quotes. Newtonsoft.Json can serialize or ...
Read more >
Try the new System.Text.Json source generator - .NET Blog
This allows the ILLinker to trim out JSON converter types which will not be needed in the application at runtime. Similarly, inspecting input...
Read more >
Feedback - Please add System.Text.Json support
Json support would be greatly appreciated. The Unity serialization system (even the new one) lacks features. Newtonsoft.Json is too random and ...
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