runtime: Unable to load assemblies using SDK 7.0.202 but worked on SDK 7.0.201

Description

When compiled using .NET SDK 7.0.202, code that uses Assembly.Load(AssemblyName) fails to load assemblies that exists in the application directory.

Here’s how you can reproduce the bug.

  1. Make sure you have .NET SDK 7.0.201 and 7.0.202 installed.
  2. Clone https://github.com/reduckted/LoadAssemblyBug
  3. Open PowerShell and run run.ps1.

The application is quite simple. It finds all .dll files in the current working directory, gets the assembly name from each file, then uses Assembly.Load() to load that assembly.

Use the run.ps1 script to reproduce the bug. It will first publish the application using SDK 7.0.201, then run the application. This will succeed and you will see it log the name of each assembly that it loads. Next it will publish the application using SDK 7.0.202, then run the application. This will fail for the vast majority of assemblies.

Here’s a snippet of the output when using 7.0.201:

Loading assembly: LoadAssemblyBug, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Loading assembly: Microsoft.AspNetCore.Antiforgery, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
Loading assembly: Microsoft.AspNetCore.Authentication.Abstractions, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
Loading assembly: Microsoft.AspNetCore.Authentication.Cookies, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
Loading assembly: Microsoft.AspNetCore.Authentication.Core, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
Loading assembly: Microsoft.AspNetCore.Authentication, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60

And here’s a snippet of the output when using 7.0.202:

Loading assembly: LoadAssemblyBug, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Loading assembly: Microsoft.AspNetCore.Antiforgery, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
Could not load file or assembly 'Microsoft.AspNetCore.Antiforgery, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=AMD64'. The system cannot find the file specified.
Loading assembly: Microsoft.AspNetCore.Authentication.Abstractions, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
Could not load file or assembly 'Microsoft.AspNetCore.Authentication.Abstractions, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=AMD64'. The system cannot find the file specified.
Loading assembly: Microsoft.AspNetCore.Authentication.Cookies, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
Could not load file or assembly 'Microsoft.AspNetCore.Authentication.Cookies, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=AMD64'. The system cannot find the file specified.
Loading assembly: Microsoft.AspNetCore.Authentication.Core, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
Could not load file or assembly 'Microsoft.AspNetCore.Authentication.Core, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=AMD64'. The system cannot find the file specified.
Loading assembly: Microsoft.AspNetCore.Authentication, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
Could not load file or assembly 'Microsoft.AspNetCore.Authentication, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=AMD64'. The system cannot find the file specified.

Configuration

  • .NET version: .NET SDK 7.0.202, self-contained published application.
  • OS: Windows 10
  • Architecture: x64

I’ve tried using Microsoft.NET.Sdk.Web and Microsoft.NET.Sdk, and both exhibit the same bug.

I have not tried using a different OS.

Regression?

Yes. This works flawlessly when compiled with .NET SDK 7.0.201.

Related to dotnet/core#8285

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 1
  • Comments: 23 (14 by maintainers)

Commits related to this issue

Most upvoted comments

We got this issue with an AssemblyLoadContext. Specifically, AssemblyLoadContext.LoadFromAssemblyName rejects an AssemblyName with a ProcessorArchitecture other than None (when the project output is published with a RID).

Workaround to use at the start of AssemblyLoadContext.Load, in case it may help anyone:

#pragma warning disable SYSLIB0037
assemblyName = (AssemblyName)assemblyName.Clone();
assemblyName.ProcessorArchitecture = ProcessorArchitecture.None;
#pragma warning restore SYSLIB0037