You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在.NET SDK风格.csproj中设置BuildInParallel="false"

解决多目标框架并行构建时PreBuild脚本冲突问题

直接在.csproj中禁用并行构建

要让Visual Studio默认禁用该项目的多目标框架并行构建,只需在项目文件的<PropertyGroup>节点中添加BuildInParallel属性:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>net6.0;net8.0</TargetFrameworks>
    <!-- 全局禁用当前项目的多目标并行构建 -->
    <BuildInParallel>false</BuildInParallel>
  </PropertyGroup>

  <!-- 你的PreBuild目标定义 -->
  <Target Name="PreBuild" BeforeTargets="PreBuildEvent">
    <Exec Command="make_zip.bat" />
  </Target>

  <!-- 确保scripts.zip被输出到构建目录 -->
  <ItemGroup>
    <Content Include="scripts.zip">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
</Project>

这个设置会覆盖Visual Studio和dotnet CLI的默认并行行为,无需每次手动添加-p:BuildInParallel=false参数。

替代方案:仅执行一次PreBuild打包

如果不想完全禁用并行构建,也可以让打包脚本只执行一次(比如仅针对其中一个目标框架),避免重复操作导致文件占用:

<Target Name="PreBuild" BeforeTargets="PreBuildEvent" Condition="'$(TargetFramework)' == 'net8.0'">
  <Exec Command="make_zip.bat" />
</Target>

或者通过增量构建判断,只有当脚本文件更新时才重新打包:

<Target Name="PreBuild" BeforeTargets="PreBuildEvent" 
        Inputs="@(ScriptFiles)" Outputs="scripts.zip">
  <ItemGroup>
    <!-- 定义需要监控的脚本文件 -->
    <ScriptFiles Include="**/*.py;**/*.bat" />
  </ItemGroup>
  <Exec Command="make_zip.bat" />
</Target>

这样既保留了多目标框架的并行构建优势,又避免了打包脚本的冲突问题。

内容的提问来源于stack exchange,提问作者Livius

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 11:20:01