0

I've been having a hard time unloading an assembly loaded as an application part. The assembly is loaded in a collectible load context, but when I try to unload it I can see in the modules window it's never been removed. I assume there is some reference left, which prevents the load context to unload. I unload the context as suggested in the MS docs:

            var weakRef = new WeakReference(context, trackResurrection: true);

            context.Unload();

            for (int i = 0; weakRef.IsAlive && (i < 10); i++)
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }

This is how I remove the application part:

                partManager.ApplicationParts.Remove(applicationPart);
                CustomActionDescriptorChangeProvider.Instance.HasChanged = true;
                CustomActionDescriptorChangeProvider.Instance.TokenSource.Cancel();

I can see that the ActionDescriptionCollection updates and the actions from the unloaded application part are removed.

I inspected pretty much all cache services but I did not notice any references to the unloaded assembly.

The load context unloads correctly if I do not use the assembly as an application part.

I also examined the heap with Windbg but I couldn't find any references to the assembly in question.

Does anyone have any idea where to look for a leftover reference?

Georgi Yankov
  • 411
  • 2
  • 9
  • Have you tried [unloading the assembly from the AppDomain](https://stackoverflow.com/questions/123391/how-to-unload-an-assembly-from-the-primary-appdomain)? And [here](https://learn.microsoft.com/en-us/dotnet/framework/app-domains/how-to-unload-an-application-domain) is the official documentation. – Chen Aug 29 '22 at 08:27
  • I am using .net core, so appdomains are not an option. I use a collectible AssemblyLoadContext. – Georgi Yankov Aug 29 '22 at 08:53
  • Have you debugged the unloadability of the assembly according to [this documentation](https://learn.microsoft.com/en-us/dotnet/standard/assembly/unloadability). – Chen Sep 20 '22 at 01:16

0 Answers0