Could not load file or assembly using .NET Core 2.0

.net-core c# html-agility-pack visual-studio

Question

I just start to write a new application with Net Core 2.0 and I have this structure:

ConsoleApp
Library

the ConsoleApp import the Library reference that is a dll, and Library import HtmlAgilityPack 1.8.0 that's the last version available.

When I run the ConsoleApp, I get this error:

Unhandled exception of type 'System.IO.FileNotFoundException' in Library.dll Could not load file or assembly 'HtmlAgilityPack, Version=1.8.0.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a'. The specified file could not be found.

The file is available, as you can see in the image:

enter image description here

Accepted Answer

Use Fuslogvw.exe (Assembly Binding Log Viewer) to determine what is exactly happening

The Assembly Binding Log Viewer displays details for assembly binds. This information helps you diagnose why the .NET Framework cannot locate an assembly at run time. These failures are usually the result of an assembly deployed to the wrong location, a native image that is no longer valid, or a mismatch in version numbers or cultures. The common language runtime's failure to locate an assembly typically shows up as a TypeLoadException in your application.

Important

You must run fuslogvw.exe with administrator privileges.

This tool is automatically installed with Visual Studio. To run the tool, use the Developer Command Prompt

For more details visit:

https://docs.microsoft.com/en-us/dotnet/framework/tools/fuslogvw-exe-assembly-binding-log-viewer

The following sample log entry shows detailed information about a failed assembly bind.

> *** Assembly Binder Log Entry  (3/5/2007 @ 12:54:20 PM) ***  
> 
> The operation failed.   Bind result: hr = 0x80070002. The system
> cannot find the file specified.  
> 
> Assembly manager loaded from: 
> C:\WINNT\Microsoft.NET\Framework\v2.0.50727\fusion.dll   Running under
> executable  C:\Program
> Files\Microsoft.NET\FrameworkSDK\Samples\Tutorials\resourcesandlocalization\graphic\cs\graphicfailtest.exe
> 
> --- A detailed error log follows.   
> 
> === Pre-bind state information ===   LOG: DisplayName = graphicfailtest.resources, Version=0.0.0.0, Culture=en-US,
> PublicKeyToken=null    (Fully-specified)   LOG: Appbase = C:\Program
> Files\Microsoft.NET\FrameworkSDK\Samples\Tutorials\resourcesandlocalization\graphic\cs\
> LOG: Initial PrivatePath = NULL   LOG: Dynamic Base = NULL   LOG:
> Cache Base = NULL   LOG: AppName = NULL   Calling assembly :
> graphicfailtest, Version=0.0.0.0, Culture=neutral,
> PublicKeyToken=null.  
> ===  
> 
> LOG: Processing DEVPATH.   LOG: DEVPATH is not set. Falling through to
> regular bind.   LOG: Policy not being applied to reference at this
> time (private, custom, partial, or location-based assembly bind).  
> LOG: Post-policy reference: graphicfailtest.resources,
> Version=0.0.0.0, Culture=en-US, PublicKeyToken=null   LOG: Attempting
> download of new URL file:///C:/Program
> Files/Microsoft.NET/FrameworkSDK/Samples/Tutorials/resourcesandlocalization/graphic/cs/graphicfailtest.resources.DLL.
> LOG: Attempting download of new URL file:///C:/Program
> Files/Microsoft.NET/FrameworkSDK/Samples/Tutorials/resourcesandlocalization/graphic/cs/graphicfailtest.resources/graphicfailtest.resources.DLL.
> LOG: Attempting download of new URL file:///C:/Program
> Files/Microsoft.NET/FrameworkSDK/Samples/Tutorials/resourcesandlocalization/graphic/cs/graphicfailtest.resources.EXE.
> LOG: Attempting download of new URL file:///C:/Program
> Files/Microsoft.NET/FrameworkSDK/Samples/Tutorials/resourcesandlocalization/graphic/cs/graphicfailtest.resources/graphicfailtest.resources.EXE.
> LOG: All probing URLs attempted and failed.

Popular Answer

Seems that I was able to fix the "bug". So for summarize, I have two application, the first one is a console, and the second is a NET CORE library, both are based on NET Core. So I removed the second app NET CORE library, and I created the same project as STANDARD NET library.

I got the same problem, when I run the console app the compiler displayed the same message error:

Unhandled exception of type 'System.IO.FileNotFoundException' in Library.dll Could not load file or assembly 'HtmlAgilityPack, Version=1.8.0.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a'. The specified file could not be found.

So I tried to install the HtmlAgilityPack also on the Console application, and as like dark magic this worked perfectly.

So I guess that I should install the dependency used on the library imported to the Console, also on the Console application because NET Core must be sure that all the dependency of the library imported are compatible with NET Core.

IMHO this should be specified by Microsoft, and again, this problem should be handled by Visual Studio itself, why I need to install the dependency twices?

Of course I'm not going to accept my answer, I'll waiting someone that explain this situation now that the problem is more clear to me, and of course, to so community.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why