Skip to content

Commit

Permalink
Fix app domain unloading issue on .NET Framework
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Nov 2, 2021
1 parent 639530f commit e2b3361
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
10 changes: 10 additions & 0 deletions CommunityToolkit.Mvvm/Messaging/Internals/System/Gen2GcCallback.cs
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
2 changes: 2 additions & 0 deletions tests/CommunityToolkit.Mvvm.UnitTests/Test_Messenger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ void Test()
Assert.IsTrue(messenger.IsRegistered<MessageA>(recipient));
}

#if NETCOREAPP // Auto-trimming is disabled on .NET Framework
[TestCategory("Mvvm")]
[TestMethod]
public void Test_Messenger_AutoCleanup()
Expand Down Expand Up @@ -515,6 +516,7 @@ void Test()

GC.KeepAlive(messenger);
}
#endif

[TestCategory("Mvvm")]
[TestMethod]
Expand Down

0 comments on commit e2b3361

Please sign in to comment.