I'm working on a project which has dependencies like this:
App references ServiceStarter
ServiceStarter references AppServiceCore
AppService references AppServiceCore
AppService references AppServiceInterface
*** I CANNOT CHANGE HOW THESE PROJECTS REFERENCE EACH OTHER SO PLEASE KEEP THAT IN MIND**
In AppStarter I don't want to reference AppService directly. Because that reference can be changed(which AppService to use, AppService1, AppService2 etc...).
How I decided to import the service project is by using a MSBuild Task like so:
<!-- only change ServicesImplProjectName to set the service implementation project -->
<PropertyGroup>
<ServicesImplProjectName>Services</ServicesImplProjectName>
<ServicesImplProjectPath>$(SolutionDir)$(ServicesImplProjectName)</ServicesImplProjectPath>
<ServiceImplProjectFileType>csproj</ServiceImplProjectFileType>
<!-- vbproj, csproj ... -->
</PropertyGroup>
<Target Name="CustomServiceStarterBeforeBuild" BeforeTargets="BeforeBuild">
<MSBuild Projects="$(ServicesImplProjectPath)\$(ServicesImplProjectName).$(ServiceImplProjectFileType);"
Targets="Restore;Build;"
BuildInParallel="false"
Properties="BuildProjectReferences=true;Configuration=debug;Platform=$(Platform);SolutionDir=$(SolutionDir);"
StopOnFirstFailure="true">
</MSBuild>
<ItemGroup>
<ServicesImplFiles Include="$(ServicesImplProjectPath)\$(OutDir)\**\*.*" />
<Content Include="@(ServicesImplFiles)">
<Link>%(RecursiveDir)%(Filename)%(Extension)</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Target>
The problem is that by using the msbuild cli and passing (while being in the ServiceStarter folder):
msbuild -p:BuildProjectReferences=true -p:Configuration=debug -target:build ServiceStarter.csproj -p:SolutionDir=MyPAth\
everything works and I get "build successful", but when I try to build using VisualStudio 2022 UI's build action (right click on ServiceStarter and then click on build), I get the error that says:
CSC : error CS0006: Metadata file 'MyPath\ServiceInterface\bin\Debug\Common.dll' could not be found
whilst it's a dependency of AppService (using project references). But why does this happen? why using msbuild CLI works but visualstudio2022 (also tested on 2019) UI build button doesn't work??
Any, I mean any helps are appreciated!
- This project uses .net4.8 if that matters.
- you can actually reproduce this exact same problem, by creating an empty solution using .net4.8 and adding 5 projects with the exact same dependency graph. then adding my custom MSBuild task to your App.csproj file.