Skip to content

Commit

Permalink
Merge pull request CommunityToolkit#4386 from Sergio0694/feature/nct-…
Browse files Browse the repository at this point in the history
…712-changes

Fix app domain unloading issue on .NET Framework
  • Loading branch information
Sergio0694 authored Nov 18, 2021
2 parents b20c6ce + 22ca6da commit 4a09bf0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ private Gen2GcCallback(Action<object> callback, object target)
/// <param name="target">The target object to pass as argument to <paramref name="callback"/>.</param>
public static void Register(Action<object> 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);
}

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

0 comments on commit 4a09bf0

Please sign in to comment.