Search code examples
c#visual-studiovisual-studio-2017.net-standard-2.0

Visual Studio projects copy dependencies from referenced project into output


Is it possible to make Visual Studio to copy all dependencies of referenced projects into the output path?

Example

In the Solution, Project A (Library, .NET Standard) defines some functions and is dependent on Library L1 (via NuGet) and Library L2 (local .dll, referenced and copied to project)

Project B (Console Application) references Project A.

When building B, The output folder contains all direct dependencies of B and A.dll. L1 and L2 are not available in the output. Therefore, the program does not work correctly.

How can I force VS to copy also L1 and L2 to the output of B?

The only way I found so far is packing A as NuGet, but this seems to be unnecessary overhead and uncomfortable. I think I am just forgetting something everyone else seems to know...

Edit (clearifying Example)

My solutions consists of two projects.

Project MongoWrapper

  • .NET Standard 2.0 class Library
  • depends on NuGet MongoDB.Driver package
  • Actually uses this dependency (no zombie dependency)

Project ConsoleUser

  • .Net Framework 4.6.1 Console Application
  • References MongoWrapper project
  • Actually uses MongoWrapper

Observation

When debugging the ConsoleUser application, it compiles and starts. During runtime, when it calls a method in the MongoWrapper which uses the MongoDB.Driver, the application crashes, as the MongoDB.Driver dependency was not copied into the output folder of the ConsoleUser.

How to fix this?


Solution

  • The problem was introduced by the usage of .Net Standard library and a .Net Framework application.

    TLDR

    Open the .csproj file of the .Net Framework project with a text editor. Inside the first PropertyGroup add the line

    <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
    

    Save the file, reopen Solution in Visual Studio and perform Clean & Build

    Dependencies in different project file versions

    .Net Framework projects use an old version of the .csproj project files. References/Dependencies are stored in the additional packages.configfile. By default, building a .Net Framework project makes the system to search for a packages.config file in the referenced projects. If no such file is found, the build task treats the referenced project as having no dependencies. Therefore, in the example, the MongoDB.Driver library is not added.

    By adding the proposed line in the .csproj project file, the build task searches the project file of the referenced project for dependencies, where they are stored in .Net Standard project files.

    .Net Core projects by default search for the newer project file structure.

    The default behavior for new projects can be set in the Options -> NuGet -> General -> Package Management