diff --git a/CommunityToolkit.Mvvm/Messaging/Internals/System/Gen2GcCallback.cs b/CommunityToolkit.Mvvm/Messaging/Internals/System/Gen2GcCallback.cs index c8a07e05c..be54bb163 100644 --- a/CommunityToolkit.Mvvm/Messaging/Internals/System/Gen2GcCallback.cs +++ b/CommunityToolkit.Mvvm/Messaging/Internals/System/Gen2GcCallback.cs @@ -41,6 +41,16 @@ private Gen2GcCallback(Action callback, object target) /// The target object to pass as argument to . public static void Register(Action callback, object target) { +#if NETSTANDARD2_0 + if (RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework")) + { + // On .NET Framework using a GC callback causes issues with app domain unloading, + // so the callback is not registered if that runtime is detected and just ignored. + // Users on .NET Framework will have to manually trim the messenger, if they'd like. + return; + } +#endif + _ = new Gen2GcCallback(callback, target); } diff --git a/tests/CommunityToolkit.Mvvm.UnitTests/Test_Messenger.cs b/tests/CommunityToolkit.Mvvm.UnitTests/Test_Messenger.cs index 14a9a593e..9c298b8be 100644 --- a/tests/CommunityToolkit.Mvvm.UnitTests/Test_Messenger.cs +++ b/tests/CommunityToolkit.Mvvm.UnitTests/Test_Messenger.cs @@ -468,6 +468,7 @@ void Test() Assert.IsTrue(messenger.IsRegistered(recipient)); } +#if NETCOREAPP // Auto-trimming is disabled on .NET Framework [TestCategory("Mvvm")] [TestMethod] public void Test_Messenger_AutoCleanup() @@ -515,6 +516,7 @@ void Test() GC.KeepAlive(messenger); } +#endif [TestCategory("Mvvm")] [TestMethod]