FileNotFoundException when referencing DLL in .NET Core Application

Viewed 7483

I have a .NET Core console application and a .NET Core class library. Both are extremely simple, single class projects. Both are freshly built, with a fresh install of the latest .NET Core. Both target .NET Core 1.1.

This error occurs at runtime whenever I include a .NET Core class library in an application:

System.IO.FileNotFoundException: 'Could not load file or assembly 'NAME, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

Building either projects are fine, and Intellisense shows me the contents of the class library after including a using... statement. With the reference and code written there are no issues at compile time.

I've set the Copy Local to Yes for the referenced assembly in my console application. The referenced DLL exists in the bin folder of the console application during run time.

Here is the csproj reference:

<Reference Include="NAME">
  <HintPath>path\bin\Debug\netcoreapp1.1\NAME.dll</HintPath>
  <Private>true</Private>
  <SpecificVersion>false</SpecificVersion>
</Reference>

This only happens with .NET Core DLLs, I have absolutely no issues with .NET Framework 4.5.* and up.

Could anybody shed some light on this issue? Any SO/MSDN pages I've found regarding this have been specific problems like targeted the incorrect version of a DLL, which doesn't help.

3 Answers

After lots of digging, I found a solution for .NET Core 2.0 that works for me.

The core issue: I added the project references through Visual Studio 2017. In my web application, I referenced two .NET Core libraries; while everything compiles, at runtime, I get a FileNotFound exception pointing to one of the two DLLs.

The solution that worked for me:

  • Close Visual Studio
  • Open the .csproj with the references in it. Delete the references to the projects.
  • From a terminal, cd into the project folder and add the references by hand, using dotnet add reference ..\..\foo\bar.csproj
  • Start Visual Studio, build and run your (web) application

For me, this resolved the issue.

Related