0

我正在开发一个 ASP.NET Core 3.1 Web 应用程序项目,我注意到一键式 Web 部署(我有一个文件夹配置文件设置)不起作用,因为我的网站文件夹中的 DLL 正在被另一个进程使用(可能是 w3wp.exe)。我对 ASP.NET Core 还是很陌生,但这对我来说对于普通的 ASP.NET 来说从来都不是问题。无论如何,我发现回收应用程序池可以暂时修复它,直到 1-2 发布后它再次发生。

我发现一个SO 帖子建议让项目的构建后事件在发布之前自动回收应用程序池,所以我这样实现:

ECHO ON
%SYSTEMROOT%\System32\inetsrv\appcmd recycle apppool /apppool.name:"NeatF2P"
dotnet publish --output C:\Websites\NeatF2P --force --no-build
pause

问题是,它并不总是有效。我构建,我"NeatF2P" successfully recycled在输出窗口中看到,然后我仍然得到同样的错误。这是完整的输出日志:

Build started...
1>------ Build started: Project: NeatF2PWebsite, Configuration: Debug Any CPU ------
1>NeatF2PWebsite -> C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\bin\Debug\netcoreapp3.1\NeatF2PWebsite.dll
1>NeatF2PWebsite -> C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\bin\Debug\netcoreapp3.1\NeatF2PWebsite.Views.dll
1>"NeatF2P" successfully recycled
1>Microsoft (R) Build Engine version 16.9.0+57a23d249 for .NET
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>
1>C:\Program Files\dotnet\sdk\5.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(238,5): warning MSB3026: Could not copy "obj\Debug\netcoreapp3.1\NeatF2PWebsite.dll" to "C:\Websites\NeatF2P\NeatF2PWebsite.dll". Beginning retry 1 in 1000ms. The process cannot access the file 'C:\Websites\NeatF2P\NeatF2PWebsite.dll' because it is being used by another process.  [C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj]
1>C:\Program Files\dotnet\sdk\5.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(238,5): warning MSB3026: Could not copy "obj\Debug\netcoreapp3.1\NeatF2PWebsite.dll" to "C:\Websites\NeatF2P\NeatF2PWebsite.dll". Beginning retry 2 in 1000ms. The process cannot access the file 'C:\Websites\NeatF2P\NeatF2PWebsite.dll' because it is being used by another process.  [C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj]
1>C:\Program Files\dotnet\sdk\5.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(238,5): warning MSB3026: Could not copy "obj\Debug\netcoreapp3.1\NeatF2PWebsite.dll" to "C:\Websites\NeatF2P\NeatF2PWebsite.dll". Beginning retry 3 in 1000ms. The process cannot access the file 'C:\Websites\NeatF2P\NeatF2PWebsite.dll' because it is being used by another process.  [C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj]
1>C:\Program Files\dotnet\sdk\5.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(238,5): warning MSB3026: Could not copy "obj\Debug\netcoreapp3.1\NeatF2PWebsite.dll" to "C:\Websites\NeatF2P\NeatF2PWebsite.dll". Beginning retry 4 in 1000ms. The process cannot access the file 'C:\Websites\NeatF2P\NeatF2PWebsite.dll' because it is being used by another process.  [C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj]
1>C:\Program Files\dotnet\sdk\5.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(238,5): warning MSB3026: Could not copy "obj\Debug\netcoreapp3.1\NeatF2PWebsite.dll" to "C:\Websites\NeatF2P\NeatF2PWebsite.dll". Beginning retry 5 in 1000ms. The process cannot access the file 'C:\Websites\NeatF2P\NeatF2PWebsite.dll' because it is being used by another process.  [C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj]
1>C:\Program Files\dotnet\sdk\5.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(238,5): warning MSB3026: Could not copy "obj\Debug\netcoreapp3.1\NeatF2PWebsite.dll" to "C:\Websites\NeatF2P\NeatF2PWebsite.dll". Beginning retry 6 in 1000ms. The process cannot access the file 'C:\Websites\NeatF2P\NeatF2PWebsite.dll' because it is being used by another process.  [C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj]
1>C:\Program Files\dotnet\sdk\5.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(238,5): warning MSB3026: Could not copy "obj\Debug\netcoreapp3.1\NeatF2PWebsite.dll" to "C:\Websites\NeatF2P\NeatF2PWebsite.dll". Beginning retry 7 in 1000ms. The process cannot access the file 'C:\Websites\NeatF2P\NeatF2PWebsite.dll' because it is being used by another process.  [C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj]
1>C:\Program Files\dotnet\sdk\5.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(238,5): warning MSB3026: Could not copy "obj\Debug\netcoreapp3.1\NeatF2PWebsite.dll" to "C:\Websites\NeatF2P\NeatF2PWebsite.dll". Beginning retry 8 in 1000ms. The process cannot access the file 'C:\Websites\NeatF2P\NeatF2PWebsite.dll' because it is being used by another process.  [C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj]
1>C:\Program Files\dotnet\sdk\5.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(238,5): warning MSB3026: Could not copy "obj\Debug\netcoreapp3.1\NeatF2PWebsite.dll" to "C:\Websites\NeatF2P\NeatF2PWebsite.dll". Beginning retry 9 in 1000ms. The process cannot access the file 'C:\Websites\NeatF2P\NeatF2PWebsite.dll' because it is being used by another process.  [C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj]
1>C:\Program Files\dotnet\sdk\5.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(238,5): warning MSB3026: Could not copy "obj\Debug\netcoreapp3.1\NeatF2PWebsite.dll" to "C:\Websites\NeatF2P\NeatF2PWebsite.dll". Beginning retry 10 in 1000ms. The process cannot access the file 'C:\Websites\NeatF2P\NeatF2PWebsite.dll' because it is being used by another process.  [C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj]
1>C:\Program Files\dotnet\sdk\5.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(238,5): error MSB3027: Could not copy "obj\Debug\netcoreapp3.1\NeatF2PWebsite.dll" to "C:\Websites\NeatF2P\NeatF2PWebsite.dll". Exceeded retry count of 10. Failed.  [C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj]
1>C:\Program Files\dotnet\sdk\5.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(238,5): error MSB3021: Unable to copy file "obj\Debug\netcoreapp3.1\NeatF2PWebsite.dll" to "C:\Websites\NeatF2P\NeatF2PWebsite.dll". The process cannot access the file 'C:\Websites\NeatF2P\NeatF2PWebsite.dll' because it is being used by another process. [C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj]
1>Press any key to continue . . .
1>C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj(24,5): error MSB3073: The command "ECHO ON
1>C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj(24,5): error MSB3073: %SYSTEMROOT%\System32\inetsrv\appcmd recycle apppool /apppool.name:"NeatF2P"
1>C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj(24,5): error MSB3073: dotnet publish --output C:\Websites\NeatF2P --force --no-build
1>C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj(24,5): error MSB3073: pause" exited with code 1.
1>Done building project "NeatF2PWebsite.csproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

如果我等待一分钟然后再次尝试构建,有时它会工作,有时它不会。手动(在 IIS 中)回收应用程序池有时会起作用,尽管它不应该做任何命令不做的事情,对吧?为什么会这样?

4

2 回答 2

1

回收就像重新启动它,它可能会在另一个构建尝试获取它之前获取文件的句柄,或者没有。发布后停止并重新启动

于 2021-09-16T13:53:12.017 回答
1

@L.Trabacchin 的回答让我走上了正轨。如果我修改我的构建后事件命令以在dotnet publish命令之后停止并重新启动应用程序池:

ECHO ON
%SYSTEMROOT%\System32\inetsrv\appcmd stop apppool /apppool.name:"NeatF2P"
dotnet publish --output C:\Websites\NeatF2P --force --no-build
%SYSTEMROOT%\System32\inetsrv\appcmd start apppool /apppool.name:"NeatF2P"

这还没有失败,我已经做了十几个构建。

于 2021-09-16T14:13:17.437 回答