FileNotFoundException when using reflection with 3rd party dll's

rwgfm

New member
Joined
Sep 27, 2023
Messages
2
Programming Experience
1-3
Hello,
I am porting an old project from Framework 4.61 to dotnet6. There exists a kind of plugin system using reflection.
C#:
var tempAssembly = System.Reflection.Assembly.LoadFile(dllFilename);
var instance = (BaseService)tempAssembly.CreateInstance(assemblyName);

This worked as long as no other dll is needed in the plugin, because as soon as another dll (custom or 3rd party) is needed, a
System.IO.FileNotFoundException: Could not load file or assembly. is triggered
The dll's definitely exists in the correct directory. Under Framework 4.61 everything works correctly.
 
The dll's definitely exists in the correct directory. Under Framework 4.61 everything works correctly.

Chances are that DLL exists, but it also has other dependencies and those dependencies do not exist, or do not work in .NET (Core).

Also did you verify that your new .NET (Core) application is running with the same bit-ness as your original .NET Framework 4.6.1 version? (e.g. your .NET Framework 4.6.1 version was compiled and run as 32-bit, but you are trying to build and run your .NET version as 64-bit.)
 
Got it.
Instead of
C#:
var tempAssembly = System.Reflection.Assembly.LoadFile(dllFilename);
it works with LoadFrom
C#:
var tempAssembly = System.Reflection.Assembly.LoadFrom(dllFilename);
 
From the documentation for LoadFile():
Use the LoadFile method to load and examine assemblies that have the same identity, but are located in different paths. LoadFile does not load files into the load-from context, and does not resolve dependencies using the load path, as the LoadFrom method does. LoadFile is useful in this limited scenario because LoadFrom cannot be used to load assemblies that have the same identities but different paths; it will load only the first such assembly.

Emphasis mine.

To me that suggests that your "load path"s are different .NET Framework 4.6.1 and .NET.
 

Latest posts

Back
Top Bottom