I am trying to add native notifications to an Avalonia app using Microsoft.Toolkit.Uwp.Notifications version 7.1.3 in a .net 8 app targeting windows. There are warnings or errors being shown when i run a dotnet publish using AOT. I have been trying to preserve all resources and stop them from being trimmed but I can't seem to figure out why they keep getting trimmed, or its something else.

The app works fine when published as a single file or not trimmed, I am wondering if its at all possible to even use this library with AOT.

my csproj ive tried with trim mode link and partial:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFrameworks>net8.0</TargetFrameworks>
    <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>
    <Nullable>enable</Nullable>
    <BuiltInComInteropSupport>true</BuiltInComInteropSupport>
    <ApplicationManifest>app.manifest</ApplicationManifest>
    <Version>0.0.1.7</Version>
    <IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
    <IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
    <ApplicationIcon>noborder.ico</ApplicationIcon>
    <AssemblyName>DesktopApp</AssemblyName>
    <PublishAot>true</PublishAot>
    <TrimMode>link</TrimMode> 
    <JsonSerializerIsReflectionEnabledByDefault>true</JsonSerializerIsReflectionEnabledByDefault>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Avalonia.Desktop" Version="11.0.5" />
  </ItemGroup>
  <ItemGroup>
     <TrimmerRootDescriptor Include="MyRoots.xml" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\desktopapp\desktopapp.csproj" />
  </ItemGroup>
</Project>

Heres my roots file (ive been trying a lot)

<linker>
  <assembly fullname="Microsoft.Toolkit.Uwp.Notifications" preserve="All" />
  <assembly fullname="Microsoft.Toolkit.Uwp.Notifications" Dynamic="Required All" />
  <assembly fullname="Microsoft.Windows.SDK.Contracts"  preserve="All"/>
  <assembly fullname="Windows.Data.Xml.Dom"  preserve="All"/>
  <assembly fullname="Windows.Data.Xml.Dom"  Dynamic="Required All"/>
  <assembly fullname="NotificationsExtensions.WinRT"  Dynamic="Required All"/>
  <assembly fullname="NotificationsExtensions.WinRT"  preserve="All"/>
</linker>

Here is the trimming message/errors:

ILC: Method '[Microsoft.Toolkit.Uwp.Notifications]Microsoft.Toolkit.Uwp.Notifications.CAppResolver..ctor()' will alwa
  ys throw because: Invalid IL or CLR metadata in 'Void Microsoft.Toolkit.Uwp.Notifications.CAppResolver..ctor()'
  ILC: Method '[PresentationFramework]System.Windows.Documents.MsSpellCheckLib.RCW+SpellCheckerFactoryCoClass..ctor()'
  will always throw because: Invalid IL or CLR metadata in 'Void SpellCheckerFactoryCoClass..ctor()'
  ILC: Method '[PresentationFramework]System.Windows.Documents.MsSpellCheckLib.RCW+SpellCheckerFactoryCoClass.Unregiste
  rUserDictionary(string,string)' will always throw because: Invalid IL or CLR metadata in 'Void SpellCheckerFactoryCoC
  lass.UnregisterUserDictionary(System.String, System.String)'
  ILC: Method '[PresentationFramework]System.Windows.Documents.MsSpellCheckLib.RCW+SpellCheckerFactoryCoClass.RegisterU
  serDictionary(string,string)' will always throw because: Invalid IL or CLR metadata in 'Void SpellCheckerFactoryCoCla
  ss.RegisterUserDictionary(System.String, System.String)'
  ILC: Method '[PresentationFramework]System.Windows.Documents.MsSpellCheckLib.RCW+SpellCheckerFactoryCoClass.CreateSpe
  llChecker(string)' will always throw because: Invalid IL or CLR metadata in 'ISpellChecker SpellCheckerFactoryCoClass
  .CreateSpellChecker(System.String)'
  ILC: Method '[WindowsBase]MS.Internal.Security.AttachmentService+AttachmentServices..ctor()' will always throw becaus
  e: Invalid IL or CLR metadata in 'Void AttachmentServices..ctor()'
  ILC: Method '[PresentationCore]MS.Internal.AppModel.CustomCredentialPolicy+InternetSecurityManager..ctor()' will alwa
  ys throw because: Invalid IL or CLR metadata in 'Void InternetSecurityManager..ctor()'
  ILC: Method '[System.DirectoryServices]System.DirectoryServices.UnsafeNativeMethods+PropertyEntry..ctor()' will alway
  s throw because: Invalid IL or CLR metadata in 'Void PropertyEntry..ctor()'
  ILC: Method '[System.DirectoryServices]System.DirectoryServices.UnsafeNativeMethods+PropertyValue..ctor()' will alway
  s throw because: Invalid IL or CLR metadata in 'Void PropertyValue..ctor()'

Here is the code that where I am showing the notification:

using desktopapp.ViewModels;
using Avalonia.Controls;
using Microsoft.Toolkit.Uwp.Notifications;
using System;

namespace desktopapp.Views.Pages;

public partial class BookmarksPage : UserControl
{
    public BookmarksPage()
    {
        InitializeComponent();
        DataContext = new BookmarksPageViewModel();
    }

    private void Button_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
#if WINDOWS
       var toast = new ToastContentBuilder()
             .AddArgument("action", "viewConversation")
             .AddArgument("conversationId", 9813)
             .AddText("Andrew sent you a picture")
             .AddText("Check this out, The Enchantments in Washington!")
             .SetToastDuration(ToastDuration.Short);

        toast.Show(toast =>
        {
            toast.ExpirationTime = DateTime.Now.AddSeconds(10);
        });
#endif
    }
}

Here is the runtime error:

 ---> System.InvalidProgramException: Common Language Runtime detected an invalid program. The body of method 'Void Microsoft.Toolkit.Uwp.Notifications.CAppResolver..ctor()' is invalid.
   at Internal.Runtime.TypeLoaderExceptionHelper.CreateInvalidProgramException(ExceptionStringID, String) + 0x40
   at Internal.Runtime.CompilerHelpers.ThrowHelpers.ThrowInvalidProgramExceptionWithArgument(ExceptionStringID, String) + 0x9
   at Microsoft.Toolkit.Uwp.Notifications.CAppResolver..ctor() + 0x15
   at Microsoft.Toolkit.Uwp.Notifications.Win32AppInfo.Get() + 0x4a
   at Microsoft.Toolkit.Uwp.Notifications.ToastNotificationManagerCompat.Initialize() + 0x8a
   at Microsoft.Toolkit.Uwp.Notifications.ToastNotificationManagerCompat..cctor() + 0x87

I have also been getting a "Failed initializing notifications" or that "ToastNotificationManagerCompat unavailable for this platform" , so I assume there is more going on that I can't quite figure out.

Any ideas on this? Could this library just not be trimmed or used with AOT?

0

There are 0 best solutions below