23

I've ran into a weird problem.

I basically have my own web-stack for .NET-core which I've built into a few .dlls, and I want to reference these from another ASP CORE-solution.

VS seems to find the assemblies, where I can navigate types etc. I can also build the project without any issues, but when IIS then runs the server I get an internal server error stating:

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

The referenced DLLs are in the debug-folder, and the really weird thing is that if I create a new ASP Core project in the same solution as the web-stack, I can reference and use it without any problems.

Why is this happening only when running on a project outside the web-stack's solution, and what can I do to make it runnable everywhere?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Tokfrans
  • 1,939
  • 2
  • 24
  • 56
  • 3
    Have you set up your reference to be copied to the output folder? (see properties of the reference) – Tseng May 14 '17 at 17:55
  • @Tseng Yes I tried this. Did not make a difference sadly – Tokfrans May 14 '17 at 20:57
  • And you're sure you didn't tried to reference a `net4xx` library in an `netcoreapp1.x` project? – Tseng May 14 '17 at 21:01
  • @Tseng Affirmative. Both the library and the web-project is targeting **.NETCoreApp 1.1** – Tokfrans May 14 '17 at 21:05
  • Libraries shouldn't usually have to target `netcoreapp1.x`, with the very rare exception of requiring .NET Core only API which is unavailable in `netstandard1.x`. You should always try to target `netstandard` – Tseng May 14 '17 at 21:14
  • @Tseng Would this make a difference though, since the answer below was the solution. Wouldn't there still have been a problem with my libraries' dependencies? – Tokfrans May 14 '17 at 21:18
  • If all libraries use and target `netstandard` there wouldn't be issues. `netstandard` libraries can be used in `netcoreapp`'s, but not other way around. `netstandard` can also be consumed from `net452` alls, but `netcoreapp` can't – Tseng May 14 '17 at 21:25

2 Answers2

36

The .NET Core Tooling in VS 2017 (< 15.3 preview) / .NET CLI < 2.0 doesn't fully support referencing assemblies on disk. You need to package the library up as a NuGet package or use a project reference ("same solution"). The technical reason is that all the required assemblies and versions are resolved during compilation and written to the .deps.json file. When loading arbitrary assemblies, this might fail because either the assembly or its dependencies cannot be found (or a conflict with each other).

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
  • Makes sense. I'll try this and get back – Tokfrans May 14 '17 at 20:59
  • 10
    This was it. I ticked the *Generate NuGet package on build*-box under the project-properties -> package, and installed the packages locally to my new web-project. – Tokfrans May 14 '17 at 21:17
  • I've been searching for a couple of days for this exact answer. Is this anywhere in the dotnetcore, dotnet cli, nuget, msbuild, or csproj documentation? The only thing I see in any of those is where it says to either add a package reference or a reference to another project. I guess it's sort of implied, but it would be nice to have some sort of statement for those of us trying to add on-disk assemblies. Thanks a ton for this answer; really helpful. – greyseal96 Jul 21 '17 at 04:20
  • I am using the VS 2017 v15.3.5, has this been fixed in this version of the VS yet? got the same issue here. – khoailang Oct 08 '17 at 16:48
  • 2
    Getting the same issue in v15.4.0 – Vesuvian Oct 10 '17 at 18:25
  • Note that there are additional issues in ASP.NET Core 2.0.0 using referenced dll files in razor views that are about to be fixed in a servicing release. – Martin Ullrich Oct 10 '17 at 18:29
  • Getting the same issue in v15.9.5. Any solution? @Tokfrans can you redirect me in the right direction. – Kishan Vaishnav Mar 29 '19 at 10:41
0

for load the external dll in core project, needed to:

  1. vs 2017 with version 15.3 or upper.
  2. Microsoft.Extensions.DependencyModel NuGet package installed in your project.
AminRostami
  • 2,585
  • 3
  • 29
  • 45