Skip to content

AssemblyLoadContext.Unload() not closing assembly file handle in .Net 5.0 Preview 6 #39609

Description

@Xalcon

Description

I'm currently testing the .Net 5.0 Preview 6 with some code that was written for .Net Core 3.1, specifically the AssemblyLoadContext's unloading capabilities.
Unloading assemblies does seem to work in 5.0, but the runtime does not close the file handle - the same code works fine in .net core 3.1.5. A minimal example project can be found here: https://github.com/Xalcon/DotNet5AssemblyContextIssue

The solution can be build with dotnet build. This will create 2 folders, one for each framework inside $(SolutionDir)/Out/Debug/.
The binary DotnetAssemblyUnloading.exe will attempt to load the PluginAssembly.dll from the same directory and execute the EntryPont.Main() function in a new collectable AssemblyLoadContext. Afterwards it will unload the context and attempt to delete the PluginAssembly.dll. This fails with an System.UnauthorizedAccessException since the assembly is still opened by the runtime - even though the assembly context was collected.

Configuration

  • Which version of .NET is the code running on? .NET 5.0.0-preview.6.20305.6
  • What OS and version, and what distro if applicable? Windows 10 Version 2004 (OS Build 19041.388)
  • What is the architecture (x64, x86, ARM, ARM64)? x64
  • Do you know whether it is specific to that configuration? I've not tested other systems or configurations.

Regression?

  • Did this work in a previous build or release of .NET Core, or from .NET Framework?
    The code is working perfectly fine on the .Net Core 3.1.5 runtime. Unloading closes the file handle, allowing the file to be deleted/replaced with a newer version.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions