"FileNotFoundException - Could not load file or assembly" from a project's library, even if LoadFrom() is used

0

I have a VS2017 project composed by:

- Main console application `App.exe`, compiled in .NET Framework 4.6.1
--- (referenced by `App.exe`) Custom dynamic library `LibOne.dll`, compiled in .NET Standard 2.0
----- (referenced by `LibOne.dll`) Another dynamic library `LibTwo.dll`, compiled in .NET Framework 3.5

By default, both LibOne.dll and LibTwo.dll are copied in App.exe folder when compiling. Instead, I need to prevent LibTwo.dll to be copied, making the program look for it from another path of the system, C:\Path_To_LibTwo.

So I've set "Copy Local" and "Specific Version" to FALSE for LibTwo.dll, in both App and LibOne projects, now the program throws this error just when I call a method of LibOne.dll:

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in App.exe
Could not load file or assembly 'LibTwo, Version=12.4.0.61, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

This is legit, but the problem still exists even if:

  • C:\Path_To_LibTwo (where LibTwo.dll is contained) has been added to System's PATH
  • I've added this line as first thing of my App: System.Reflection.Assembly.LoadFrom(@"C:\Path_To_LibTwo\LibTwo.dll"); (Note: this line correctly returns the LibTwo's assembly, so it is found)

UPDATE: I added this to App.config:

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
        <assemblyIdentity name="LibTwo" culture="neutral" publicKeyToken="null" />
        <codeBase href="file:///C:/Path_To_LibTwo/LibTwo.dll" />
    </dependentAssembly>
</assemblyBinding>

And now the error is different:

Could not load file or assembly 'LibTwo, Version=12.4.0.61, Culture=neutral, PublicKeyToken=null' or one of its dependencies. 
The private assembly was located outside the appbase directory. (Exception from HRESULT: 0x80131041)
c#
.net
visual-studio
dll
dynamic-loading
asked on Stack Overflow Jan 28, 2020 by TheUnexpected • edited Jan 28, 2020 by TheUnexpected

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0