14

I'm having an issue with VS2019 profesional 16.5.5 and 16.5.4 with my C# multi-project solution. It compiles, tests and executes correcly, but in the background I have the constant restore of nuget packages, completing once every few seconds. The CPU is close to 100%.

I have tried Update-Package -reinstall, nuget locals all --clear (from: How do I fix this NuGet Restart loop in Visual Studio 2017?), updating VS, removing my .vs folder, cleaning the solution, checking out the project again from git and even restarting. Nothing seems to help.

The project has a few main projects: two console, one AspNetCore, one WPF. They use common libs in Standard 2.0 or shared C# projects.

The Package Manager log in Output window has hundereds of entries like:

Time Elapsed: 00:00:00.7441577
========== Finished ==========

Time Elapsed: 00:00:00.8448774
========== Finished ==========

Time Elapsed: 00:00:00.8410551
========== Finished ==========

Time Elapsed: 00:00:00.7351174
========== Finished ==========

Time Elapsed: 00:00:00.7997720
========== Finished ==========

Time Elapsed: 00:00:00.9367757
========== Finished ==========

Unloading some projects helps a bit, but only temporarily - until I load them again.

How can I fix the constant Nuget package restore issue?

Krzysztof Bociurko
  • 4,575
  • 2
  • 26
  • 44
  • Please try to disable any third party extensions under `Extensions`-->`Manage Extensions` in case it interferes with nuget performance. 2) enter Tools-->Options-->Projects and Solutions-->Build and Run-->Set `MSBuild project build output verbosity` to `Detailed` to see the detailed log about nuget restore and then find out what's wrong. – Mr Qian May 14 '20 at 10:00
  • Manage Extensions - don't have any third party extensions. MSBuild verbose log didn't help (even with diagnostic mode) - could not find reason why restore was being triggered. I did find the reason by other means, though, currently writing as an answer. – Krzysztof Bociurko May 14 '20 at 11:05

2 Answers2

20

I found what causes the issue. Some time ago I tried adding auto versioning to the console projects (with help of Auto Versioning in Visual Studio 2017 (.NET Core)). To be exact, I added to my .csproj file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <!-- ... -->
    <VersionPrefix>1.1</VersionPrefix>
    <VersionSuffix>$([System.DateTime]::UtcNow.ToString(yyyyMMddTHHmmss))</VersionSuffix>
  </PropertyGroup>
  <!-- ... -->
</Project>

This was the direct cause of the issue. When commenting out the dynamic expression in VersionSuffix, the issue disappeared. What is more interesting, changing to yyyyMMddTHHmm (removed ss) also fixed this. And no, it did not change the frequency of Nuget restore to minutely - it stopped completely.

Even more, I have found a post on Microsoft Developer forums that details the same issue. The post has been since removed, but I managed to archive it for purposes of this answer. The post has just been moved: Using current datetime as VersionSuffix in csproj causes restoring of packages forever .

Krzysztof Bociurko
  • 4,575
  • 2
  • 26
  • 44
  • 4
    It's a known problem: https://learn.microsoft.com/en-us/nuget/release-notes/nuget-4.4-rtm#a-package-in-a-net-core-project-that-contains-an-assembly-with-an-invalid-signature-can-trigger-an-infinite-restore-loop It's a consequence of the auto-building design of the new sdk projects. Issue here: https://github.com/dotnet/project-system/issues/1457 – imps May 15 '20 at 10:13
  • Thanks, I'll link this to the dotnet issue. – Krzysztof Bociurko May 15 '20 at 10:16
  • Thank you so much, almost wanted to report this as a bug. – AyCe Nov 04 '20 at 17:27
  • I believe it doesn't occur minutely because it rarely ends up changing during the previous package restore. – Denise Skidmore Dec 23 '21 at 16:36
  • Thanks same think i have done and wonder why its continue to restoring. thanks – Dharmeshsharma Apr 13 '22 at 13:57
6

I know you found where the issue was coming from, but here is the fix, for those who want to keep the seconds in their version number.

Add the following to your .csproj:

<PropertyGroup>
    <VersionPrefix>1.0.0</VersionPrefix>
    <VersionSuffix Condition="'$(DesignTimeBuild)' != 'true' OR '$(BuildingProject)' == 'true'">beta-$([System.DateTime]::UtcNow.ToString(yyyyMMdd-HHmmss))</VersionSuffix>
</PropertyGroup>

It nicely updates all various places with the right versioning string, including your .dll and NuGet, only when building the project (and not at design time).

sw1337
  • 533
  • 4
  • 16