在this解决方案 我有 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'
Node :
运行项目 build-script
或 node ./build-script/app.js
为什么 Shared
项目甚至使用 --no-dependencies
标志构建?如何并行或同时构建?
最佳答案
错误 CS2012 的解释:进程无法访问文件
我可以重现您的问题和 Process Monitor表示两个进程同时打开shared.dll
进行读取。这会导致您描述的问题。
尽管可以通过使用 FileShare.Read
作为 documentation 从不同的进程读取相同的文件。表明(请参阅下面的摘录),这似乎不是由 csc.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
-switch 启用并行构建:
dotnet msbuild .\ParallelBuildAB.csproj /target:build /restore:false /maxcpucount:2
此外,还需要在项目文件 ParallelBuildAB.csproj 中使用 BuildInParallel
-task 参数。请注意,引用了 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>
关于node.js - 并行或同时构建 dotnet 核心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47895162/