7

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?

auX
  • 860
  • 9
  • 19

2 Answers2

11

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

auX
  • 860
  • 9
  • 19
  • 1
    This has solved my problem. Thanks you very much!!! A year later and the problem is still there... :-( – Tolbxela Jul 30 '19 at 12:36
  • this worked for me, but it seemed to wipe out nuget management. I *think* I need to remove the nuget packages I had from references and add them again – Garr Godfrey Dec 14 '21 at 08:34
2

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

Yes.

This is what publishing the application does - it prepares the application for deployment. When you publish, it will include all of the dependencies that the application requires to run in the output.

Use the Publish tool to deploy to a local folder. The exact options available depend on your app type. In Solution Explorer, right-click your project and choose Publish, and then choose Folder. For more information, see Deploy to a local folder.

enter image description here

Tutorial: Publish your Hello World application with Visual Studio 2017

Also see: .NET Core application deployment.

Community
  • 1
  • 1
NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • 1
    tested this approach against the clarified example from above. It does not work as expected. In addition, this is not suitable for debugging – auX Mar 12 '18 at 09:17