错误:该进程无法访问文件,因为在.NET Core上通过CLI构建项目时,该文件正被另一个进程使用

Kir*_*ahi 5 .net c# .net-core

dotnet build在.NET Core项目上运行命令时出现以下错误。

C:\Program Files\dotnet\sdk\2.1.2\Microsoft.Common.CurrentVersion.targets(4106,5
 ): warning MSB3026: Could not copy "obj\Debug\netcoreapp2.0\Blog-Yantra.dll" to
 "bin\Debug\netcoreapp2.0\Blog-Yantra.dll". Beginning retry 1 in 1000ms. The proc
 ess cannot access the file 'E:\learn\blog\Blog-Yantra\bin\Debug\netcoreapp2.0\Bl
 og-Yantra.dll' because it is being used by another process.  [E:\learn\blog\Blog
 -Yantra\Blog-Yantra.csproj]
Run Code Online (Sandbox Code Playgroud)

错误消息的屏幕截图。

我的csproj文件如下所示:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <TypeScriptToolsVersion>2.3</TypeScriptToolsVersion>
  </PropertyGroup>

  <ItemGroup>
    <Folder Include="wwwroot\" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.1" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="2.0.0" />
  </ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

Sge*_*dda 11

最有可能的是一个开放的.Net核心进程正在运行并阻塞,如果在Windows上,只需通过任务管理器杀死它即可。

在此输入图像描述


Kir*_*ahi 8

所以,在这里我想出了解决方案。
有进程正在运行和锁定程序集,因为我确实dotnet run通过 dotnet cli 运行了项目,而我做了Ctrl+c来终止正在运行的进程。但是Ctrl+c并没有杀死所有进程,Childs 继续运行并锁定程序集,即 kestrel 服务器仍在同一端口上运行。

为了杀死正在运行的 kestrel 服务器,我运行了以下命令。

C:\Users\Kiran>netstat -ano -p TCP | find /I "listening" | find /I "2492"
TCP    127.0.0.1:2492         0.0.0.0:0              LISTENING       820

C:\Users\Kiran>taskkill /F /PID 820
SUCCESS: The process with PID 820 has been terminated.
Run Code Online (Sandbox Code Playgroud)

你需要运行的命令

netstat -ano -p TCP | find /I "listening" | find /I "{your port number}"
taskkill /F /PID {your process ID}
Run Code Online (Sandbox Code Playgroud)

如何手动停止 kestrel 服务器

这个问题的一些参考是:

如何手动停止 kestrel 服务器。对 SO 提出的问题

端口 localhost:5000 未释放导致错误 System.IO.IOException: 无法绑定到地址 http://127.0.0.1:5000: 地址已在使用中。问题发布在 github 上

终止dotnet run不会终止孩子。github上的问题

Windows:如何按端口终止进程。博文