VS2022无法还原NuGet包求助(Windows Server 2022环境)
在Windows Server 2022(21H2 - OS Build 20348.2340)环境下使用VS2022还原NuGet包时,所有包均出现以下错误:
Failed to download package 'Microsoft.AspNetCore.Authentication.WsFederation.8.0.4' from
'https://api.nuget.org/v3-flatcontainer/microsoft.aspnetcore.authentication.wsfederation/8.0.4/microsoft.aspnetcore.authentication.wsfederation.8.0.4.nupkg'.
Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
An existing connection was forcibly closed by the remote host
已尝试重新安装VS、清除NuGet缓存、验证TLS配置等操作,浏览器可直接下载包但VS无法完成还原,可尝试以下解决方案:
检查VS2022代理设置
打开VS2022,依次进入「工具」→「选项」→「环境」→「Web浏览器」→「代理设置」,确认VS代理配置与系统一致。若服务器使用代理,确保VS正确配置;也可勾选「自动检测设置」,或添加api.nuget.org到代理例外列表。修改NuGet配置文件强制TLS版本
编辑用户目录下的NuGet.Config(路径:C:\Users\MyUser\AppData\Roaming\NuGet\NuGet.Config,删除过则重新生成),在<configuration>节点内添加以下配置:<config> <add key="DisableParallelProcessing" value="true" /> <add key="packageRestoreTimeout" value="300" /> </config> <runtime> <AppContextSwitchOverrides value="Switch.System.Net.DontEnableSchUseStrongCrypto=false;Switch.System.Net.DontEnableSystemDefaultTlsVersions=false" /> </runtime>禁用并行包还原
方法一:在项目根目录新建或编辑Directory.Build.props文件,添加内容:<Project> <PropertyGroup> <RestoreDisableParallel>true</RestoreDisableParallel> </PropertyGroup> </Project>方法二:打开VS的「工具」→「选项」→「NuGet包管理器」→「包还原」,取消勾选「允许并行还原多个项目的包」。
排查防火墙与杀毒软件干扰
临时禁用Windows Defender防火墙或第三方杀毒软件,尝试再次还原。若恢复正常,需将VS2022(devenv.exe)和NuGet进程(nuget.exe)加入防火墙允许列表,并允许api.nuget.org的出站连接。更新.NET环境
安装最新版.NET Framework 4.8.1及项目对应版本的.NET SDK(如.NET 8.0最新版),旧版本SDK可能存在TLS兼容性问题。命令行手动还原
打开VS2022开发者命令提示符,进入项目根目录执行:nuget restore YourProject.sln -DisableParallelProcessing -Timeout 300若命令行成功还原,可尝试重置VS设置(「工具」→「导入和导出设置」→「重置所有设置」)修复VS内部配置问题。
内容的提问来源于stack exchange,提问作者77Vetter

