-
Notifications
You must be signed in to change notification settings - Fork 135
[BUG] Assembly.Load() no longer works for unreferenced dlls in .NET 5+ #466
Description
According to dotnet runtime issue 62522, you can no longer use Assembly.Load() to load DLLs that aren't reference by the project in .NET 5+. Here is the relevant quote:
.NET 5/6+ has a simpler and much more strict policy about finding assemblies to load. It does not search for files on disk if they were not part of the original project. In this case the the SampleAssembly is in the same directory, but the app will not try to load it as it wasn't built with it.
This means that Load will not work (as the default assembly resolution will not look for additional files on disk). On the other hand LoadFrom might work (since it is given a file path). As written it's depending on current directory, which is rather fragile.
This change between .NET Framework and .NET 5 was intentional. The goal is to make application behavior more predictable - in this case, adding new files to the directory with the app doesn't change runtime behavior (unlike in .NET Framework).
The result is that if you add an unreferenced serilog DLL to your applications directory, Serilog will detect it by reading the files off of the disk, but fail to load it at the Assembly.Load() call and throw an exception in .NET 5+. This is a pain to debug, because it often happens only in production (for example, when a serilog dependency gets removed) and breaks logging if you are using Serilog, so you have no insights (ask me how I know).