How to solve a Failure of NuGet restore (impossible to access a deleted object) from the Package Manager of Visual Studio 2019

visual studio nuget restore

This post deals with a case of error occurring during the restoration of NuGet packages of a solution under Visual Studio 2019 (for example for a project recovered from a git repository) and which contains in the .csproj guidelines
<package>.

The restoration is triggered automatically during the first compilation of the project, if it fails it is impossible to compile the project.

Of course there are many reasons why a restoration can fail: packages not available for hardware architecture, project is targeting outdated versions of packages, source of packages that are unavailable (bad configuration, network problem), …

What to do if you get the “Can’t Access a Deleted Object” error?

This error, which appears in the log of the package manager’s restoration operation built into Visual Studio, is joined with errors such as “Package Download Failure”, “Cannot Resolve Assembly” , “New attempt at ‘FindPackagesByIdAsync’ and consecutive errors indicating that projects (dlls) are not found, as in the following example:

An error occurred when restoring NuGet packages: Failed to
recover information on ‘runtime.win10-x86.Microsoft.Net.UWPCoreRuntimeSdk’ from the remote source ‘https://api.nuget.org/v3-flatcontainer/runtime.win10-x86.microsoft.net.uwpcoreruntimesdk/index.json’.
1>– Beginning of Generation: Project: XamlControlsGallery, Configuration: Debug x86 —— 1:-Users-franc-Documents-Visual Studio 2019-Projects-testnugetrestorefail
Xaml-Controls-Gallery-master-XamlControlsGallery-XamlControlsGallery.csproj: XamlCompiler error WMC1006: Cannot resolve Assembly or Windows Metadata file ‘Type universe cannot resolve assembly: System.Runtime, Version 4.0.0.0, Culture-neutral,PublicKeyToken-b03f5f7f11d50a3a.’ 1st:SCC error CS2001: Source file ‘C:’Users’franc-Documents’Visual Studio 2019-Projects-testnugetrestorefail-Xaml-Controls-Gallery-master’XamlControlsGallery-obj-x86-Debug-App.g.i.cs’

======== Generation: 0 succeeded, 1 failed, 0 updated, 0 was ignored ========
Project build report:
Status  Project [Config-platform]
——-|——-|—————–

Failed | XamlControlsGallery.csproj [Debug-x86]
Failed to download the package ‘runtime.win10-x86.Microsoft.Net.Native.Compiler.2.1.8’ from
‘https://api.nuget.org/v3-flatcontainer/runtime.win10-x86.microsoft.net.native.compiler/2.1.8/runtime.win10-x86.microsoft.net.native.compiler.2.1.8.nupkg’.
Can’t access a deleted object.
Object name: ‘SslStream’.

We can launch the command Package-Update (targeting the project concerned with the problem) into the Visual Studio package manager’s console (which at least allows you to see in detail the operations performed) but that doesn’t change anything. You don’t have to look long, try to change the version of the targeted packages, change the target architectures or the configuration of the nuget sources…

In fact it is a package manager crash related to a simple bug!

Fortunately, there is a simple and effective solution: perform the operation manually using nuget.exe:

1) Not being integrated with Visual Studio’s tool suite, start by downloading the latest version of the tool on the dedicated page of the site nuget.org: https://www.nuget.org/download. The latest version is: nuget.exe – recommended latest v5.4.0

2) copy the file nuget.exe in the project directory (next to the .csproj)

3) open your favorite shell in the path containing the file

4) launch the command nuget restore

nuget restore
MSBuild self-detection: using msbuild version ‘16.5.0.12403’ from ‘C:’Program Files (x86)’- Microsoft Visual Studio’2019-Community-MSBuild-Current-bin’.
Restoring packages for C:\Users\ [] .csproj…
GET 
https://api.nuget.org/v3-flatcontainer/microsoft.net.native.compiler/index.json
GET https://api.nuget.org/v3-flatcontainer/microsoft.net.uwpcoreruntimesdk/index.json
GET https://api.nuget.org/v3-flatcontainer/colorcode.uwp/index.json OK
https://api.nuget.org/v3-flatcontainer/microsoft.net.native.compiler/index.json 537ms
GET https://api.nuget.org/v3-flatcontainer/microsoft.net.native.compiler/2.1.8/microsoft.net.native.compiler.2.1.8.nupkg
[]
Committing restore…
Generating MSBuild file C:\Users\ 
[] .csproj.nuget.g.props.
Generating MSBuild file C:\Users\ 
[.csproj.nuget.g.targets.
Writing assets file to disk. Path: C: ‘Users’ 
[] \obj\project.assets.json
Restore
completed in 23.26 min for C:-Users [] csproj.
animated dance smiley image

5) You can now open the project in Visual Studio and compile it

Leave a Reply