(C#) 构建多个目标框架时出错:"Cannot open 'MyApi.dll' for writing"
(C#) Error with building multiple target frameworks: "Cannot open 'MyApi.dll' for writing"
我正在关注 this guide 使用 NSwag 自动生成一个 API 客户端。但是这个客户端需要支持多个目标框架:
<TargetFrameworks>netcoreapp2.2;net452;net462;net472;net48</TargetFrameworks>
当我尝试构建此客户端时,出现多个错误,如下所示:
(CS2012) Cannot open 'MyApi.dll' for writing -- 'The process cannot access the file 'MyApi.dll' because it is being used by another process.'
我怀疑这是因为每个框架都是异步构建的,并且从我的 API 生成的 DLL 正试图被每个进程读取。我该如何解决这个问题/让每个目标框架同步构建?
好吧,我找到了解决办法。我对 the guide 缺乏理解,我正在阅读,构建过程意味着我没有问正确的问题。
我在我的 .csproj 中指定了这个构建目标(按照指南中的指示):
<Target Name="NSwag" BeforeTargets="PrepareForBuild" Condition="'$(GenerateCode)'=='True' ">
<Exec Command="$(NSwagExe_Core22) run nswag.json /variables:Configuration=$(Configuration)" />
</Target>
这个目标是 运行ning 每个目标框架 我在我的 <TargetFrameworks>
标签中指定。这是 运行 与其自身并行并导致我的问题出现错误的任务。
经过大量谷歌搜索后,我找到了 this question and (consequently this answer),这给了我所需的解决方案:
On multi target frameworks I use BeforeTargets="DispatchToInnerBuilds"
so my custom command is only exeuted once before every build
所以我的最终构建目标就这么简单:
<Target Name="NSwag" BeforeTargets="DispatchToInnerBuilds" Condition="'$(GenerateCode)'=='True' ">
<Exec Command="$(NSwagExe_Core22) run nswag.json /variables:Configuration=$(Configuration)" />
</Target>
还应注意,此解决方案可能不适用于所有寻求构建目标 运行 每个构建一次的人。请参阅 this issue comment,它提供了类似的答案,但提供了有关多项目解决方案的更多详细信息。
我正在关注 this guide 使用 NSwag 自动生成一个 API 客户端。但是这个客户端需要支持多个目标框架:
<TargetFrameworks>netcoreapp2.2;net452;net462;net472;net48</TargetFrameworks>
当我尝试构建此客户端时,出现多个错误,如下所示:
(CS2012) Cannot open 'MyApi.dll' for writing -- 'The process cannot access the file 'MyApi.dll' because it is being used by another process.'
我怀疑这是因为每个框架都是异步构建的,并且从我的 API 生成的 DLL 正试图被每个进程读取。我该如何解决这个问题/让每个目标框架同步构建?
好吧,我找到了解决办法。我对 the guide 缺乏理解,我正在阅读,构建过程意味着我没有问正确的问题。
我在我的 .csproj 中指定了这个构建目标(按照指南中的指示):
<Target Name="NSwag" BeforeTargets="PrepareForBuild" Condition="'$(GenerateCode)'=='True' ">
<Exec Command="$(NSwagExe_Core22) run nswag.json /variables:Configuration=$(Configuration)" />
</Target>
这个目标是 运行ning 每个目标框架 我在我的 <TargetFrameworks>
标签中指定。这是 运行 与其自身并行并导致我的问题出现错误的任务。
经过大量谷歌搜索后,我找到了 this question and (consequently this answer),这给了我所需的解决方案:
On multi target frameworks I use
BeforeTargets="DispatchToInnerBuilds"
so my custom command is only exeuted once before every build
所以我的最终构建目标就这么简单:
<Target Name="NSwag" BeforeTargets="DispatchToInnerBuilds" Condition="'$(GenerateCode)'=='True' ">
<Exec Command="$(NSwagExe_Core22) run nswag.json /variables:Configuration=$(Configuration)" />
</Target>
还应注意,此解决方案可能不适用于所有寻求构建目标 运行 每个构建一次的人。请参阅 this issue comment,它提供了类似的答案,但提供了有关多项目解决方案的更多详细信息。