Skip to content

Commit 4a09bf0

Browse files
authored
Merge pull request #4386 from Sergio0694/feature/nct-712-changes
Fix app domain unloading issue on .NET Framework
2 parents b20c6ce + 22ca6da commit 4a09bf0

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

Microsoft.Toolkit.Mvvm/Messaging/Internals/System/Gen2GcCallback.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ private Gen2GcCallback(Action<object> callback, object target)
4141
/// <param name="target">The target object to pass as argument to <paramref name="callback"/>.</param>
4242
public static void Register(Action<object> callback, object target)
4343
{
44+
#if NETSTANDARD2_0
45+
if (RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework"))
46+
{
47+
// On .NET Framework using a GC callback causes issues with app domain unloading,
48+
// so the callback is not registered if that runtime is detected and just ignored.
49+
// Users on .NET Framework will have to manually trim the messenger, if they'd like.
50+
return;
51+
}
52+
#endif
53+
4454
_ = new Gen2GcCallback(callback, target);
4555
}
4656

Microsoft.Toolkit.Mvvm/Messaging/WeakReferenceMessenger.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace Microsoft.Toolkit.Mvvm.Messaging
3030
/// The <see cref="WeakReferenceMessenger"/> type will automatically perform internal trimming when
3131
/// full GC collections are invoked, so calling <see cref="Cleanup"/> manually is not necessary to
3232
/// ensure that on average the internal data structures are as trimmed and compact as possible.
33+
/// Note: this is not supported when running on .NET Framework, due to app domain unloading issues.
3334
/// </para>
3435
/// </remarks>
3536
public sealed class WeakReferenceMessenger : IMessenger

0 commit comments

Comments
 (0)