简体   繁体   中英

Visual Studio 2022/dotnet build: Build and copy dependencies without project reference

My question is similar to Visual studio project build dependencies with no references? :

I have a Web API project that references some services from other projects in the same.sln file. I want their output (DependencyA.dll, DependencyB.dll) to be copied to my Web API output folder, but I don't want to add a project reference because I'm using dependency injection, so I don't want anything but the service interfaces to be available to the Web API project.

How can I make sure:

  1. Hitting F5 in VS 2022 will build the dependencies if necessary and copy them to the startup project's output folder
  2. This will also work with do.net build since that is what we use in TeamCity (an additional PowerShell script for copying the files would be fine) - I guess it should always work, as long as we run do.net build for the.sln file?

Is there a better solution than adding.sln-level project dependencies and using a post-build task for the Web API project to copy the dependencies over?

I think the solution to add build dependencies in the Web Api project and use post build event in other projects to copy the.dll files are good enough to your problem:

1. Create project dependencies in the solution .

2.Add post build event in the other projects like this:

xcopy "$(TargetDir)$(ProjectName).dll" "$(SolutionDir)WebApplication1\Output"

What do you want to improve on this method?

When we build project in visual studio, it calls MSBuild. MSBuild refers to the.proj file and some configuration files related to the project. So if you want to call or use some dll/assembly files(DependencyA.dll, DependencyB.dll) in the main project(Web Api project), you need to set relevant references/dependencies in.proj file or configuration file. Otherwise VS or MSBuild cannot find and use them.

I don't think there is a better solution about this problem. If anyone have a good solution can share it here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM