dotnet / dotnet/wpf

AssemblyLoadContext.Unload does not unload a Wpf Library

Open
#3,816 1 comment 1 reaction 0 assignees View on GitHub
Investigate
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

* .NET Core Version: 3.1.9 and .Net 5.0.100
* Windows version: 10.0.18363
* Does the bug reproduce also in WPF for .NET Framework 4.8?: Not applicable since AssemblyLoadContext is not supported

I am trying to load and unload on demand a Wpf App library (and all the related dependencies). Everything works, but none of the assemblies get unloaded when calling the Unload method.
If I replace the Wpf library with a .Net Core library containing a few sample methods, I can see the library removed from VS Modules window after a couple of GC iterations.
I am not sure if the problem I am reporting is related to [this](https://github.com/dotnet/runtime/issues/13226) and/or [this](https://github.com/dotnet/wpf/issues/1764).
I attached a sample project which is structured like this:

_Project 1 (MainApp, a console project with added System.Windows.Forms reference to enable message pump)_
```
class Program
{
class WpfAppAssemblyLoadContext : AssemblyLoadContext
{
public WpfAppAssemblyLoadContext() : base(true) { }
protected override Assembly Load(AssemblyName assemblyName) => null;
}

[MethodImpl(MethodImplOptions.NoInlining),]
public static void TestRun()
{
var context = new WpfAppAssemblyLoadContext();
var assembly = context.LoadFromAssemblyPath($"{Environment.CurrentDirectory}\\WpfLibrary.dll");
var inst = (IProxy) assembly.CreateInstance("WpfLibrary.MainWindow");
inst.ShowWindow();
inst.CloseWindow();
context.Unload();
assembly = null;
context = null;
inst = null;
}

[STAThread,]
static void Main(string[] args)
{
TestRun();
for (var i = 0; i < 100; i++) {
GC.Collect();
GC.WaitForPendingFinalizers();
}

Application.Run();
}
}
```
_Project 2 (ProxyInterface)_
```
namespace ProxyInterface
{
public interface IProxy
{
void ShowWindow();
void CloseWindow();
}
}
```
_Project 3 (a regular wpf library with implementation of interface in Project 2 )_
```
namespace WpfLibrary
{
public partial class MainWindow : Window, IProxy
{
public MainWindow()
{
InitializeComponent();
}

public void ShowWindow() { Show();}
public void CloseWindow() { Close();}
}
}
```
[UnloadWpfLibrary.zip](https://github.com/dotnet/wpf/files/5543910/UnloadWpfLibrary.zip)
(Solution file inside "MainApp" folder)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.