代码之家  ›  专栏  ›  技术社区  ›  Wilhelmina Lohan

dotnet核心并行或同时构建

  •  8
  • Wilhelmina Lohan  · 技术社区  · 7 年前

    在里面 this 解决方案I有2个应用程序: AppA , AppB 共享类库 Shared . 我已经尝试将这些的构建/运行与 PowerShell 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: ./建筑ps1 enter image description here

    节点: 运行项目 build-script node ./build-script/app.js enter image description here

    为什么 共享 项目建设,即使 --无依赖项 旗帜如何并行或同时构建?

    1 回复  |  直到 7 年前
        1
  •  5
  •   Aedvald Tseh    7 年前

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

    我可以复制你的问题 Process Monitor 指示两个进程都已打开 shared.dll 同时进行阅读。这导致了您描述的问题。

    虽然可以通过使用 FileShare.Read 作为 documentation 表明(见下面的摘录),这似乎是 不是 完成人 csc.exe . 这是 csc。exe文件 ,在不久的将来可能不会改变。

    允许随后打开文件进行读取。如果此标志不是 指定任何打开文件进行读取的请求(通过此过程 或其他进程)将失败,直到文件关闭。然而,即使 如果指定了此标志,其他权限可能仍然是 需要访问该文件。

    解决方案

    而不是使用 Start-Process 要实现并行构建,可以使用msbuild,它支持这种开箱即用的方式。

    msbuild支持并行生成 /maxcpucount -开关和 BuildInParallel -任务参数,如中所述 MS build documentation .

    通过使用启用并行生成 /maxcpucount -打开命令行:

    dotnet msbuild。\ParallelBuildAB。csproj/目标:生成/还原:false /maxcpucount:2

    此外,还必须使用 建筑并行 -项目文件ParallelBuildAB中的任务参数。csproj公司。请注意 AppA.csproj AppB.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>