Visual Studio projects copy dependencies from referenced project into output
Asked Answered
M

2

8

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?

Myiasis answered 7/3, 2018 at 8:40 Comment(2)
Be more precise, create a demonstration (minimal reproducible example)Soluble
It should normally work, besides maybe old-school GAC usage. So do list all platforms, versions, packages etc.Soluble
M
14

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

Myiasis answered 12/3, 2018 at 12:44 Comment(3)
This has solved my problem. Thanks you very much!!! A year later and the problem is still there... :-(Rackrent
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 againTurpentine
This setting completely solved my problem. Just converted several projects to .net standard 2.0 format with the new sdk project type and the dependencies were no longer copying to the older .net framework projects until I added this setting to the older project files.Ferrite
L
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.

Laoag answered 7/3, 2018 at 10:31 Comment(1)
tested this approach against the clarified example from above. It does not work as expected. In addition, this is not suitable for debuggingMyiasis

© 2022 - 2024 — McMap. All rights reserved.