© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
2 replies
ParaLogia

❔ How to dynamically load a different version of an assembly in the NET Core shared framework

Hi, I'm trying to dynamically load an assembly from a file 'System.Threading.Channels.dll', in a NET 6 project. But it throws an exception when trying to load the file.

Minimum reproducible example below.

Sandbox.csproj:
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>
</Project>

Program.cs:
using System;
using System.IO;
using System.Reflection;

var directory = @"C:\path\to\directory\containing\the\file"; 
var dllName = "System.Threading.Channels.dll";
var assemblyPath = Path.Combine(directory, dllName);
Assembly.LoadFrom(assemblyPath); 
// System.IO.FileLoadException: 'Could not load file or assembly 'System.Threading.Channels, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.'
using System;
using System.IO;
using System.Reflection;

var directory = @"C:\path\to\directory\containing\the\file"; 
var dllName = "System.Threading.Channels.dll";
var assemblyPath = Path.Combine(directory, dllName);
Assembly.LoadFrom(assemblyPath); 
// System.IO.FileLoadException: 'Could not load file or assembly 'System.Threading.Channels, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.'

The
Assembly.LoadFrom
Assembly.LoadFrom
call throws the
FileLoadException
FileLoadException
in the comment above. Full stack trace:
   at System.Runtime.Loader.AssemblyLoadContext.LoadFromPath(IntPtr ptrNativeAssemblyLoadContext, String ilPath, String niPath, ObjectHandleOnStack retAssembly)
   at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath)
   at System.Reflection.Assembly.LoadFrom(String assemblyFile)
   at Program.<Main>$(String[] args) in C:\Users\me\source\repos\Sandbox\Sandbox\Program.cs:line 8
   at System.Runtime.Loader.AssemblyLoadContext.LoadFromPath(IntPtr ptrNativeAssemblyLoadContext, String ilPath, String niPath, ObjectHandleOnStack retAssembly)
   at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath)
   at System.Reflection.Assembly.LoadFrom(String assemblyFile)
   at Program.<Main>$(String[] args) in C:\Users\me\source\repos\Sandbox\Sandbox\Program.cs:line 8

DLL properties in screenshot below.

For this example, I can fix it if I add a
PackageReference
PackageReference
to
"System.Threading.Channels" Version="7.0.0"
"System.Threading.Channels" Version="7.0.0"
OR if I change the
TargetFramework
TargetFramework
to
net7.0
net7.0
. But neither of these options are possible in the actual codebase.

I can explain more about the actual context if needed, but right now I'm just trying to understand why this happens.
details.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Automatic Assembly Versioning in .NET Core
C#CC# / help
2y ago
Searching for the best way to dynamically load and unload assemblies in .NET Framework
C#CC# / help
2y ago
❔ Migrate AppDomain (.net framework) to AssemblyLoadContext (.net core)
C#CC# / help
3y ago