You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调试期间出现异常后Visual Studio 2017需重启才能正常构建

搞定VS2017调试异常后无法构建的MSB3061权限拒绝问题

问题还原

你在使用**Visual Studio 2017(15.7.1版本)**调试基于.NET Framework 4.6.2的WebApi项目时碰上个糟心事儿:调试时抛出异常后,再去构建项目就一堆MSB3061权限拒绝错误,说没法删除bin目录下的exe和dll文件,非得重启VS才能正常构建。你的项目还依赖了一个自定义类库,这个类库又用到了Entity Framework 6、AutoMapper和LinqKit。

错误日志片段如下:

1>Build FAILED. 1> 1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(5033,5): warning MSB3061: Unable to delete file "C:\PATH_TO_PROJECT_FOLDER\bin\Debug\net462\WebApiApplication.exe". Access to the path 'C:\PATH_TO_PROJECT_FOLDER\bin\Debug\net462\WebApiApplication.exe' is denied. 1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(5033,5): warning MSB3061: Unable to delete file "C:\PATH_TO_PROJECT_FOLDER\bin\Debug\net462\AutoMapper.dll". Access to the path 'C:\PATH_TO_PROJECT_FOLDER\bin\Debug\net462\AutoMapper.dll' is denied. 1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(5033,5): warning MSB3061: Unable to delete file "C:\PATH_TO_PROJECT_FOLDER\bin\Debug\net462\EntityFramework.SqlServer.dll". Access to the path 'C:\PATH_TO_PROJECT_FOLDER\bin\Debug\net462\EntityFramework.SqlServer.dll' is denied. 1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(5033,5): warning MSB3061: Unable to delete file "C:\PATH_TO_PROJECT_FOLDER\bin\Debug\net462\EntityFramework.dll". Access to the path 'C:\PATH_TO_PROJECT_FOLDER\bin\Debug\net462\EntityFramework.dll' is denied. 1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(5033,5): warning MSB3061: Unable to delete file "C:\PATH_TO_PROJECT_FOLDER\bin\Debug\net462\Microsoft.AspNetCore.DataProtection.dll". Access to the path 'C:\PATH_TO_PROJECT_FOLDER\bin\Debug\net462\Microsoft.AspNetCore.DataProtection.dll' is denied. 1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(5033,5): warning MSB3061: Unable to delete file "C:\PATH_TO_PROJECT_FOLDER\bin\Debug\net462\Microsoft.AspNetCore.Diagnostics.dll". Access to the path 'C:\PATH_TO_PROJECT_FOLDER\bin\Debug\net462\Microsoft.AspNetCore.Diagnostics.dll' is denied. 1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(5033,5): warning MSB3061: Unable to delete file "C:\PATH_TO_PROJECT_FOLDER\bin\Debug\net462\Microsoft.AspNetCore.Hosting.dll". Access to the path 'C:\PATH_TO_PROJECT_FOLDER\bin\Debug\net462\Microsoft.AspNetCore.Hosting.dll' is denied. 1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(5033,5): warning MSB3061: Unable to delete file "C:\PATH_TO_PROJECT_FOLDER\bin\Debug\net462\Microsoft.AspNetCore.Http.dll". Access to the path 'C:\PATH_TO_PROJECT_FOLDER\bin\Debug\net462\Microsoft.AspNetCore.Http.dll' is denied. 1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(5033,5): warning MSB3061: Unable to delete file "C:\PATH_TO_PROJECT_FOLDER\bin\Debug\net462\Microsoft.AspNetCore.Http.Abstractions.dll". Access to the path 'C:\PATH_TO_PROJECT_FOLDER\bin\Debug\net462\Microsoft.AspNetCore.Http.Abstractions.dll' is denied. 1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(5033,5): warning MSB3061: Unable to delete file "C:\PATH_TO_PROJECT_FOLDER\bin\Debug\net462\Microsoft.AspNetCore.Mvc.Abstractions.dll". Access to the path 'C:\PATH_TO_PROJECT_FOLDER\bin\Debug\net462\Microsoft.AspNetCore.Mvc.Abstractions.dll' is denied. 1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(5033,5): warning MSB3061: Unable to delete file "C:\PATH_TO_PROJECT_FOLDER\bin\Debug\net462\Microsoft.AspNetCore.Mvc.Core.dll". Access to the path 'C:\PATH_TO_PROJECT_FOLDER\bin\Debug\net462\Microsoft.AspNetCore.Mvc.Core.dll' is denied. 1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(5033,5): warning MSB3061: Unable to delete file "C:\PATH_TO_PROJECT_FOLDER\bin\Debug\net462\Microsoft.AspNetCore.Mvc.Razor.dll". Access to the path 'C:\PATH_TO_PROJECT_FOLDER\bin\Debug\net462\Microsoft.AspNetCore.Mvc.Razor.dll' is denied.

实用解决方案

我整理了几个经过开发者验证的解决办法,你可以按顺序试试:

1. 先杀残留进程(最快临时解决)

调试异常后,VS经常没彻底关掉WebApi的宿主进程,这些进程会死死锁住bin目录里的文件。

  • 打开任务管理器,找这两类进程:
    • 直接搜你的项目exe名(比如WebApiApplication.exe),找到就结束它
    • 如果用IIS Express调试,就找iisexpress.exe进程,把它结束掉
  • 杀完进程再点构建,基本就能搞定,不用重启VS

2. 改下构建输出路径

有时候VS的文件监控机制会搞事情,换个输出路径可能就解锁了:

  • 右键项目 → 属性 → 生成 → 输出路径,把默认的bin\Debug\net462改成别的,比如bin\Debug\net462_build
  • 先清理解决方案,再重新构建,看看还会不会报错

3. 关掉“编辑并继续”功能

这个功能虽然方便,但偶尔会导致进程锁定文件,尤其是异常发生后:

  • 点工具 → 选项 → 调试 → 常规,取消勾选启用编辑并继续
  • 重启VS后再测试调试和构建流程

4. 升级VS2017到最新补丁

你用的15.7.1是比较早的版本,微软后续修复了不少调试和构建的bug:

  • 打开VS的“工具 → 获取工具和功能”,检查并安装所有可用更新,尽量升到15.9.x以上的版本(VS2017最终版是15.9.56)

5. 检查自定义类库的资源泄漏

你的自定义类库依赖了EF6、AutoMapper这些组件,异常时可能没正确释放资源,导致文件被锁:

  • 检查类库里有没有没关闭的数据库连接、文件流,确保异常时能通过using或者try-catch-finally释放资源
  • 把AutoMapper和LinqKit更到兼容.NET4.6.2的最新稳定版,旧版本可能存在资源泄漏问题

最后总结

临时救急就杀进程,长期解决最好升级VS补丁,同时排查下类库的资源释放问题。如果这些都没用,你可以试试新建一个空白WebApi项目,逐步迁移代码和依赖,看看是不是原项目的配置文件坏了。

内容的提问来源于stack exchange,提问作者Michael Earls

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:44:54