question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nuget package not resolved by AssemblyLoadContext.Default.LoadFromAssemblyName in NET6 with UseCommonOutputDirectory

See original GitHub issue

Description

When UseCommonOutputDirectory is enabled NET 6.0 console applications are unable to resolve nuget dependencies.

Example output below:

image

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

Repo with reproduction

Steps:

  1. Create project targeting NET 6.0
  2. Add <UseCommonOutputDirectory>true</UseCommonOutputDirectory> to csproj
  3. Add <PackageReference Include="System.Drawing.Common" Version="6.0.0" /> to an item group
  4. Attempt to load “System.Drawing.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51” using AssemblyLoadContext.Default.LoadFromAssemblyName
  5. Build & Run
  6. 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:open
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
davidjnetcommented, Jan 27, 2022

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>, although System.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?

1reaction
adwitkowcommented, Jun 14, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

could not load file or assembly exception NET 5/6 of NuGet ...
I want to believe that the problem is a version mismatch between NET 5 and NET 6 and the version of the Nuget...
Read more >
Any resources for resolving assembly dependencies at ...
I'm loading assemblies at runtime. Previously we used Assembly::LoadFrom APIs with .NET Framework so most of the time, things just worked ...
Read more >
About AssemblyLoadContext - .NET
NET Core and is not available in .NET Framework. This article supplements the AssemblyLoadContext API documentation with conceptual information.
Read more >
Unable to resolve dependencies of NuGet packages
The cause for this error is that NuGet cache conflicts with the build of the site. Solution. To resolve the problem, perform the...
Read more >
Resolving Assemblies in .NET Core
The first problem is how to load an assembly from the disk in .NET Core. AssemblyLoadContext. The System.Runtime.Loader package contains a type ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found