Nuget package not resolved by AssemblyLoadContext.Default.LoadFromAssemblyName in NET6 with UseCommonOutputDirectory
See original GitHub issueDescription
When UseCommonOutputDirectory is enabled NET 6.0 console applications are unable to resolve nuget dependencies.
Example output below:
NET 6:
6.0.1 System.IO.FileNotFoundException: Could not load file or assembly 'System.Drawing .Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified. File name: 'System.Drawing.Common, Version=6.0.0.0, Culture=neutral, PublicKeyTo ken=cc7b13ffcd2ddd51' at System.Reflection.RuntimeAssembly.InternalLoad(ObjectHandleOnStack assembl yName, ObjectHandleOnStack requestingAssembly, StackCrawlMarkHandle stackMark, B oolean throwOnFileNotFound, ObjectHandleOnStack assemblyLoadContext, ObjectHandl eOnStack retAssembly) at System.Reflection.RuntimeAssembly.InternalLoad(AssemblyName assemblyName, RuntimeAssembly requestingAssembly, StackCrawlMark& stackMark, Boolean throwOnFi leNotFound, AssemblyLoadContext assemblyLoadContext) at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyName(AssemblyNam e assemblyName) at Program.<Main>$(String[] args) in C:\Users\dominic.farrington.HQ\Dev\Assem blyLoaderRepo\System.Drawing.Common.Assembly.Test\Program.cs:line 9
NET 5:
5.0.12 Loaded: System.Drawing.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken= cc7b13ffcd2ddd51
Reproduction Steps
Steps:
- Create project targeting NET 6.0
- Add
<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
to csproj - Add
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
to an item group - Attempt to load “System.Drawing.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51” using AssemblyLoadContext.Default.LoadFromAssemblyName
- Build & Run
- Observe exception
Expected behavior
Nuget packages should be successfully resolved at runtime when UseCommonOutput is enabled and the executable is launched from the bin folder
Actual behavior
Nuget packages are not successfully resolved.
Regression?
Worked in NET 5.0
Known Workarounds
No response
Configuration
Version: NET 6.0.100 OS: Windows 10 Enterprise 21H2 19044.1415 Architecture: Any CPU
Other information
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (2 by maintainers)
Top GitHub Comments
Targetting .NET 5, the SDK generates a
.runtimeconfig.dev.json
in the output that includes%USERPROFILE%\.nuget\packages
as an additional probing path.When building with
<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
, althoughSystem.Drawing.Common.dll
is not copied to the output, the.runtimeconfig.dev.json
allows it to be found in the nuget cache..NET 6 SDK stopped generating *.runtimeconfig.dev.json because of https://github.com/dotnet/sdk/pull/17014/files which seems to have caused this regression.
@OkkioXavier if you add
<GenerateRuntimeConfigDevFile>true</GenerateRuntimeConfigDevFile>
along with<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
do you get the same behaviour as .NET 5?I’d like to report that the issue is still occurring - although following the steps provided by @davidjnet definitely help. And due to the exception being very generic (and seemingly out of nowhere, because the
UseCommonOutputDirectory
setting alone worked for me for a while), it was astoundingly difficult to stumble upon this fix.