admin管理员组

文章数量:1431943

I have a situation where I have a "mother" application written with .NET Core. It will start any number of "child" applications also written with .NET Core.

In the mother application, I have a project reference to the child applications so that it builds everything to one place. This works fine with the exception of any files that have the same filename between mother and child. Specifically, I'm worried about appsettings.json since I want a separate file for mother and for child.

My fix to this was to try to build the child project into a subdirectory of the mother project build directory. After some research, I thought I could do this by specifying a DestinationSubDirectory value on the ProjectReference in the .csproj file. However, I end up with child.dll and child.pdb in this folder and all the other files (including child.exe) in same folder as the output for the mother project rather than in the child subdirectory.

I'm assuming the solution is a pre or post build script. I'm not entirely sure how to go about that. The first thing I assume I need to do is make sure child has been built. I thought I could leave the project reference to child and set ReferenceOutputAssembly to false, but that still puts child files into the mother build directory which I didn't really want.

I also tried building child to the mother build directory using Base output path, but that ends up with something like this:

Mother\bin\Debug\net8.0\win-x64\Child\bin\Debug\net8.0\win-x64\

when I really want to put it in

Mother\bin\Debug\net8.0\win-x64\Child\

Any suggestions on what to do?

I have a situation where I have a "mother" application written with .NET Core. It will start any number of "child" applications also written with .NET Core.

In the mother application, I have a project reference to the child applications so that it builds everything to one place. This works fine with the exception of any files that have the same filename between mother and child. Specifically, I'm worried about appsettings.json since I want a separate file for mother and for child.

My fix to this was to try to build the child project into a subdirectory of the mother project build directory. After some research, I thought I could do this by specifying a DestinationSubDirectory value on the ProjectReference in the .csproj file. However, I end up with child.dll and child.pdb in this folder and all the other files (including child.exe) in same folder as the output for the mother project rather than in the child subdirectory.

I'm assuming the solution is a pre or post build script. I'm not entirely sure how to go about that. The first thing I assume I need to do is make sure child has been built. I thought I could leave the project reference to child and set ReferenceOutputAssembly to false, but that still puts child files into the mother build directory which I didn't really want.

I also tried building child to the mother build directory using Base output path, but that ends up with something like this:

Mother\bin\Debug\net8.0\win-x64\Child\bin\Debug\net8.0\win-x64\

when I really want to put it in

Mother\bin\Debug\net8.0\win-x64\Child\

Any suggestions on what to do?

Share Improve this question edited Nov 18, 2024 at 20:54 marc_s 757k184 gold badges1.4k silver badges1.5k bronze badges asked Nov 18, 2024 at 20:52 DanDan 5739 silver badges31 bronze badges 4
  • 1 Why add the project reference? These ought to be self-contained projects if the child projects need to execute independent from the parent. It does matter, you get extra copies of the referenced project's assemblies when you build the parent. – Hans Passant Commented Nov 18, 2024 at 21:20
  • It's to make sure the child gets built when the parent gets built and that the child gets packaged with the parent. While child processes could be executed without the parent, that's just for debugging purposes. Normal execution has to go through the parent. – Dan Commented Nov 18, 2024 at 21:27
  • But you could just copy the child file into the parent's output folder with a post build event. You don't have to reference the child in the parent project; you would only do that if the parent wanted to execute code contained in the child project – flackoverstow Commented Nov 19, 2024 at 0:06
  • @flackoverstow, I was worried about the order of things if I'm building the solution, but maybe I'm too paranoid. Clearly if the mother builds first, a post-build on the mother wouldn't work because the child hasn't built. If the child builds first, a post-build will copy to the mother, but I was worried about whether the mother build would clear that out. I guess it's something I probably should have tested. – Dan Commented Nov 19, 2024 at 0:14
Add a comment  | 

1 Answer 1

Reset to default 0

After looking around a bit in the Visual Studio UI, I found the right way to include the child project as a reference, but not actually copy files to the mother project.

I ended up setting Copy Local to No and Copy Local Satellite Assemblies to No. I'm not sure if I needed both of those set to No, but it seems to have done the trick.

This resulted in the following changes done to the csproj:

<ProjectReference Include="..\Child\Child.csproj">
  <Private>False</Private>
  <CopyLocalSatelliteAssemblies>False</CopyLocalSatelliteAssemblies>
</ProjectReference>

I still don't know what setting ReferenceOutputAssembly to false was supposed to do, but it wasn't what I wanted.

And second thing was the post-build event on the mother project:

echo Creating Target Directory: "$(ProjectDir)$(OutDir)Child"
mkdir "$(ProjectDir)$(OutDir)Child"
xcopy /y /e /i "..\Child\bin\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)" "$(ProjectDir)$(OutDir)Child\"

Edit 1: When I went to deploy, my MSBuild command was specifying an absolute OutDir value, so I ended up having to remove $(ProjectDir), so depending on your environment, you may or may not have to deal with this.

本文标签: cVisual Studio reference NET Core console app from another NET Core console appStack Overflow