-2

I'm deploying my .NET7 application using Assembly Trimming and am working around the trimming issues.

There's this weird issue with the list of files of type ICollectionView<FileItem>. The list items appear but only the text is missing, otherwise the app seems to work fine. Other listboxes are displaying fine.

public class FileItem
{
    public FileItem() : this(string.Empty, string.Empty)
    { }

    public FileItem(string name, string path)
    {
        Name = name;
        Path = path;
    }
    
    public string Name { get; set; }
    public string Path { get; set; }
}

Another issue is with serialization, I serialize to a settings file that contains this.

public List<SettingsPlaylistItem> Playlists { get; set; } = new();

It complains that SettingsPlaylistItem constructor is missing. Adding [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] does not help.

The only solution I found is to add this on app start. Is there a better solution?

// Solve trimming problems
var _ = new SettingsPlaylistItem();
Etienne Charland
  • 3,424
  • 5
  • 28
  • 58
  • As for constructor issue - check out [this answer](https://stackoverflow.com/a/75673682/2501279). Also full [mre] would be great. – Guru Stron Mar 17 '23 at 19:40
  • Just realized that the list of folders was displaying fine, it's the list of files that causes problems. Updated the post. – Etienne Charland Mar 17 '23 at 19:58
  • Interesting. Adding `[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(FileItem))]` on the Main method works, but not adding it on the FileItem class? – Etienne Charland Mar 17 '23 at 20:02
  • Is this application using WPF? WPF is known to not be trim compatible. – Hank Mar 18 '23 at 00:24
  • Avalonia11 is full trim-compatible – Etienne Charland Mar 18 '23 at 01:39
  • What is Avalonia11? Or Avalonia UI? To make a good question, the contents should be nice and clear. You also claimed that “full trim-compatible” without a reference. – Lex Li Mar 19 '23 at 05:06
  • Avalonia11 is the upcoming version of Avalonia UI with support for Windows, Linux, MacOS, Android, iOS and WebAssembly. It's currently in preview5; Assembly Trimming is supported in the nightly build and will be supported in preview6 which should come out very soon. – Etienne Charland Mar 19 '23 at 21:23
  • Ever since they launched Avalonia XPF, it is better to call the open source project AvaloniaUI or Avalonia UI for clarity. – Lex Li Mar 20 '23 at 00:19

1 Answers1

0

First, setting the first line on Main method works, but setting the 2nd on the type itself doesn't. Not sure why.

// Works
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(FileItem))]
public static void Main(string[] args) { }

// Does not work
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
public class FileItem
{
    public FileItem() : this(string.Empty, string.Empty)
    { }

    public FileItem(string name, string path)
    {
        Name = name;
        Path = path;
    }
    
    public string Name { get; set; }
    public string Path { get; set; }
}

Then, a better way to deal with serialization issues is to use XML Serializer Source Generation. As a note, JSon Source Generator implementation is a lot better than for XML. Using Json Source Generator, I could remove the above attributes, and trim XML support out of the app for a nice file size reduction, plus faster initialization.

I also ran into another issue of a list that wouldn't display and none of the above worked. If adding items to an ObservableCollection from a background threat, it works without trimming, but fails with trimming! The solution was to invoke the Add onto the UI threat.

I guess that there are few discussions on the topic because WPF does not support trimming. Avalonia11 supports it but it's still in preview5.

Etienne Charland
  • 3,424
  • 5
  • 28
  • 58
  • This answer in its current state is more like a personal note and less helpful to others, because it didn’t show clearly what exactly you changed (as you didn’t share then initial code either). You will have to update it to improve the quality. – Lex Li Mar 19 '23 at 05:09
  • I added a tutorial page with the various trimming issues that I encountered https://github.com/mysteryx93/Modern.Net-Tutorial/blob/main/9_AssemblyTrimming.md – Etienne Charland Mar 19 '23 at 23:12
  • That's not even an official post from Avalonia team itself. Besides, nothing from its release 11 notes indicated full compliance to trimming, https://github.com/AvaloniaUI/Avalonia/milestone/13?closed=1 Don't let the initial excitement take you over. – Lex Li Mar 20 '23 at 00:34
  • That post is mine. I already compiled and published an app with trimming successfully. v0.10.8 supports trimming with mode "copyused" that is not supported by .NET7. v11 support full trimming mode with some getchas, like requiring compiled bindings that have some issues. The devs themselves respond to all the questions in that regards, but v11 doesn't yet have documentation. Note that the only way I could get trimming to work was to find the right nightly build that doesn't have major problems, until preview6 comes out. – Etienne Charland Mar 20 '23 at 04:34
  • https://github.com/AvaloniaUI/Avalonia/issues/10483 "I think "copyused" is no-op in .NET 7. And it always will do member-level trimming. You can use "partial" instead though, but Avalonia 11 supports fully trimmed applications or fully AOT, if user code is safe too (i.e., no reflection bindings)." Also they removed all runtime references to `System.Xml` which apparently resulted in a 2mb reduction after trimming! Xaml framework that doesn't rely on Xml, hah – Etienne Charland Mar 20 '23 at 04:36
  • That's definitely nice news, but your limited test cases cannot prove that Avalonia UI is fully compliant to trimming ("is safe" is too subjective to define). And considering that XPF might distract the developers away, it would be interesting to see how they balance their efforts between the two. – Lex Li Mar 20 '23 at 04:42
  • That XPF discussion came out recently. https://github.com/AvaloniaUI/Avalonia/discussions/10653 "Note that "XPF focus" usually means adding missing features to Avalonia, since our policy is to make thin wrappers on top of Avalonia APIs when possible. It also gives us better understanding why certain features were designed in a particular way in WPF which would allow porting stuff later (people were complaining about missing Storyboard support for years and we are now investigating how they work internally and what it would take to bring those to Avalonia)." – Etienne Charland Mar 20 '23 at 05:15