15

Let's say my solution has 2 projects:

  • The first called "MainProject" (A .NETStandard 2.0 project).
  • The second called "MainProjectTests" (A NUnit test project) with some unit tests for each class into "MainProject".

The first project (MainProject) has a NuGet dependency called "dependencyX". Obviously, the project "MainProjectTests" has a reference to "MainProject".

So when the test runner runs a test of "MainProjectTests" that calls methods from "MainProject" using "dependencyX" I'm getting a System.IO.FileNotFoundException exception:

System.IO.FileNotFoundException : Could not load file or assembly 'dependencyX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.

Why am I getting this exception? When I add "dependencyX" to "MainProjectTests" all works fine, but it seems to me not a good practice... How to solve it?

I'm using Visual Studio for Mac Community 7.2 preview (7.2 build 583)

Thanks for the help.

EDIT:

Tried putting the options:

<RestoreProjectStyle>PackageReference</RestoreProjectStyle>

<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>

in the NUnit project, but getting the same result.

11
  • 1
    Why doesn't it seem like a good practice to have dependencies available where you use them? Where should the runtime get them from instead?
    – CodeCaster
    Commented Sep 14, 2017 at 14:03
  • 2
    stackoverflow.com/questions/44706516/…
    – CodeCaster
    Commented Sep 14, 2017 at 14:03
  • @CodeCaster thanks for the comment. My test project is not directly using dependencyX. Instead, it is calling a method from a class inside a project that uses dependencyX. I am wrong? So I believe the test project should not know about the existence of dependencyX... Commented Sep 14, 2017 at 14:14
  • 1
    Is MainProject set up to put the dependencyX binaries in the build output directory? If not, why would you expect it to be runnable?
    – Zastai
    Commented Sep 16, 2017 at 12:07
  • 1
    Copying the dll in the test bin works but it's a weird workaround.
    – flchaux
    Commented Nov 7, 2017 at 10:59

2 Answers 2

6
+100

This seems to be a known bug regarding .NET Standard libraries (and maybe especially in conjunction with NUnit). I already filed a bug report here which seems to confirm that this is not the intended behaviour. Altough there has been no progress for over half a year.

Maybe one should file a bug in the NUnit repo after confirming this only happens when using NUnit.

For the time beeing you'll need to reference all libraries used in a .NET Standard project also in all projects referencing the .net standard one as you are doing right now.

0
1

It is a bug reported to Microsoft few times and it seems they did not do much on this, look at this Visual Studio does not copy referenced assemblies through the reference hierarchy

On the other hand at least with Nuget Packages you have a simple way (to add the same package to multiple projects in the same solution) using the package manager for the solution, as you can see here Nuget Package Manager.

solution manager

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.