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.

System.Windows.Markup.XamlParseException in System.Runtime when published trimmed

See original GitHub issue

Description

Publishing a WPF application using the trim unused assemblies option (VS -> Build dialog) throws a System.Windows.Markup.XamlParseException on start of the application. Publishing untrimmed works fine.

Configuration

image

Other information

 OS Name:     Windows
 OS Version:  10.0.19042
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.201\

Host (useful for support):
  Version: 5.0.4
  Commit:  f27d337295
System.Windows.Markup.XamlParseException: An exception was thrown by calling the constructor for type "Aaaa.MainWindow" that matches the specified binding restrictions.

 ---> System.TypeInitializationException: The type initializer for 'System.Windows.Window' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'System.Windows.FrameworkElement' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'System.Windows.Documents.TextElement' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'System.Windows.Documents.Typography' threw an exception.
 ---> System.IO.FileNotFoundException:

File name: 'System.Runtime, Version = 5.0.0.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a'
   at System.Windows.Media.TextFormatting.TextRunTypographyProperties.OnPropertiesChanged ()
   at MS.Internal.Text.TypographyProperties.ResetProperties ()
   at MS.Internal.Text.TypographyProperties..ctor ()
   at System.Windows.Documents.Typography..cctor ()
   --- End of inner exception stack trace ---
   at System.Windows.Documents.TextElement..cctor ()
   --- End of inner exception stack trace ---
   at System.Windows.FrameworkElement..cctor ()
   --- End of inner exception stack trace ---
   at System.Windows.Window..cctor ()
   --- End of inner exception stack trace ---
   at System.Windows.Window..ctor ()
   at Aaaa.MainWindow..ctor ()
   --- End of inner exception stack trace ---
   at System.Windows.Markup.XamlReader.RewrapException (Exception e, IXamlLineInfo lineInfo, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.Load (XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.LoadBaml (XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
   at System.Windows.Markup.XamlReader.LoadBaml (Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
   at System.Windows.Application.LoadBamlStreamWithSyncInfo (Stream stream, ParserContext pc)
   at System.Windows.Application.LoadComponent (Uri resourceLocator, Boolean bSkipJournaledProperties)
   at System.Windows.Application.DoStartup ()
   at System.Windows.Application. <. ctor> b__1_0 (Object unused)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall (Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen (Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)

The trim feature has removed the following files, several of them sounds pretty much mandatory for EVERY app to me and shouldn’t be trimmed: image

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
ThomasGoulet73commented, Mar 24, 2021

Here is the target I use to configure trimming in my WPF application:

<Target Name="ConfigureTrimming" BeforeTargets="PrepareForILLink">
  <ItemGroup>
    <ManagedAssemblyToLink Condition="'%(Filename)' == 'System.Runtime' Or '%(Filename)' == 'System.Runtime.Extensions' Or '%(Filename)' == 'System.Diagnostics.Debug'">
      <IsTrimmable>false</IsTrimmable>
    </ManagedAssemblyToLink>
  </ItemGroup>
</Target>

You can copy/paste it in your main csproj and if you still have an exception, you can add the trimmed assembly in the condition using Or '%(Filename)' == 'NAME_OF_YOUR_ASSEMBLY'.

1reaction
danmoseleycommented, Mar 24, 2021

My assumption was that this was due to WPF assemblies not supporting trimming because they have not been annotated ie they have linker warnings.

@eerhardt thoughts?

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Published WPF application exception
The only useful error info I see is System.Windows.Markup.XamlParseException . I've included all the necessary assembly used in the project, ...
Read more >
Known trimming incompatibilities - .NET
Trimming relies on seeing all assemblies at build time, so it knows which code is used and cannot be trimmed away. Most plugin...
Read more >
System.Windows.Markup Namespace
Indicates to XAML processors that the whitespace surrounding elements of the type in markup should be trimmed when serializing. Implements a markup extension ......
Read more >
XamlParseException Class (System.Windows.Markup)
Represents the exception class for parser-specific exceptions from a WPF XAML parser. This exception is used in XAML API or WPF XAML parser...
Read more >
Trim self-contained applications - .NET
Learn how to trim self-contained apps to reduce their size. .NET Core bundles the runtime with an app that is published self-contained 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