并行或同时构建 dotnet 核心

dotnet core build in parallel or simultaneously

this solution I have 2 apps: AppA, AppB that share class library Shared. I have tried automating the build/running of these in parallel with both a PowerShell and node 脚本中(我愿意接受其他解决方案)。我同时使用 --no-dependencies--no-restore 标志,但我间歇性地得到:

'CSC : error CS2012: Cannot open \'C:\Users\User\source\repos\ParallelBuild\Shared\obj\Debug\netcoreapp2.0\Shared.dll\' for writing -- \'The process cannot access the file \'C:\Users\User\source\repos\ParallelBuild\Shared\obj\Debug\netcoreapp2.0\Shared.dll\' because it is being used by another process.\' [C:\Users\User\source\repos\ParallelBuild\Shared\Shared.csproj]\r\n'

PowerShell: ./build.ps1

节点: 运行 项目 build-scriptnode ./build-script/app.js

为什么 Shared 项目甚至带有 --no-dependencies 标志?如何并行或同时构建?

错误 CS2012 的解释:进程无法访问文件

我可以重现您的问题,Process Monitor 表示两个进程同时打开 shared.dll 进行读取。这会导致您描述的问题。

尽管可以使用 FileShare.Read 从不同的进程中读取相同的文件,如 documentation 所示(请参阅下面的摘录),这似乎是 NOTcsc.exe 完成。这是csc.exe的缺点,短期内应该不会改了。

Allows subsequent opening of the file for reading. If this flag is not specified, any request to open the file for reading (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions might still be needed to access the file.

解决方案

除了使用 Start-Process 来实现并行构建,您还可以使用 msbuild,它开箱即用地支持此功能。

msbuild 使用 /maxcpucount-switch 和 BuildInParallel-task 参数支持并行构建,如 MS build documentation.

中所述

通过在命令行上使用 /maxcpucount-开关启用并行构建:

dotnet msbuild .\ParallelBuildAB.csproj /target:build /restore:false /maxcpucount:2

此外还需要在项目文件ParallelBuildAB.csproj中使用BuildInParallel-task参数。请注意 AppA.csprojAppB.csproj 被引用:

<?xml version="1.0"?>
<Project name="system" default="build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Target Name="build">
        <MSBuild BuildInParallel="true" Projects="./AppA/AppA.csproj;./AppB/AppB.csproj" Targets="Build"/>
    </Target>
</Project>