30

I've been fighting with this error for several hours and can't come up with a solution that works.

I have an ASP.Net API within a multi-project solution which has its references/dependencies improperly configured and I've been trying to fix it for two days.

The issue is (I believe) that the API is missing System.Web.Http / System.Web.Http.WebHost which apparently is included with Microsoft.AspNet.WebApi.Core (per multiple other Stack Overflow questions). When trying to install it via Nuget I get the following error when attempting to resolve the dependency. Further I've gotten this error every time (see my troubleshooting later in this question):

Screenshot of Package Manager Console

I've done the following steps in no particular order (multiple times each in various combinations):

  • Run 'Update-package Microsoft.AspNet.WebApi.Core -reinstall'
  • Delete the solution-wide "Packages" folder and delete the project's packages.config and attempt to restore packages
  • Delete the entire project from the physical drive and TFS, and recreate it from scratch (it creates with the references incorrect)
  • Install the package via both the command line and Nuget GUI
  • Check the solution out to different locations on my hard drive in an attempt to start as fresh as possible
  • Reboot my computer/Restart Visual Studio

At this point I'm fine with a non-clean fix, as long as it fixes it. The API was working at one point, but then I checked it into TFS, created a branch, and am now working on the branch. Possibly some dependencies weren't included (?) but even if this was the case it seems like the steps I've taken should resolve the issue.

Ranger
  • 1,139
  • 1
  • 13
  • 34
  • 1
    Is the 'Packages' folder checked-in in TFS? – vendettamit Apr 29 '16 at 17:47
  • 2
    did you try running `update-package -reinstall` ? this command reinstalls all packages in all projects – nazarevic Apr 29 '16 at 17:48
  • @vendettamit It is. I suppose I haven't tried deleting it from TFS, but right now I'm just trying to get it to work locally. Do you think it's of merit to try deleting it from TFS? – Ranger Apr 29 '16 at 17:49
  • 2
    I once saw this problem occurring because those package folder were checked-in and re-install was failed because of check-out operation was being performed to update the package files. Remove packages folder from TFS worked. Anyways it's not recommended to keep packages in source control. – vendettamit Apr 29 '16 at 17:51
  • @nazarevic The command just completed. Or, well, didn't with the same error message. – Ranger Apr 29 '16 at 17:59
  • 1
    @vendettamit Your suggestion plus nazarevic's worked together. Thank you SO much. If either of you want to write that up as a proper answer I'll happily accept and upvote it. – Ranger Apr 29 '16 at 18:19
  • I had this same problem. Restarting Visual Studio fixed it. NuGet is riddled with bugs... – makhdumi Nov 01 '16 at 00:27
  • 1
    I had this problem in VS2015. I also had VS2013 running, doing the same command there worked fine :S – RuudvK Feb 09 '17 at 10:08
  • @vendettamit "it's not recommended to keep packages in source control". This is an understatement.One of the purposes of dependencies manager is to alleviate source code. ie you only need to source YOUR work – Bombinosh Mar 24 '17 at 17:36

5 Answers5

12

Close the solution then go to the packages folder on your hard drive and delete all its contents. Open your application again in Visual Studio and it will automatically try to restore the packages. Clean the Solution and Build it.

  • 1
    In my case, I think it was because I had just run the web application in non-debug (Ctrl+F5) mode. Restarting Visual Studio sorted the problem. – NickBeaugié Apr 21 '18 at 22:25
  • 1
    In our case this happened on our build server so had to perform this on the user that the build server uses when local dev was fine – Alan Macdonald Jul 26 '18 at 07:45
8

Clearing the cache solved the problem for me. There's an option to clear the cache on Tools > Options > NuGet Package Manager > General menu, called Clear All NuGet Cache(s).

3

First, uninstall via package manager console the specific NuGet package

Then, please unload the project where this nugget package was referenced for by right click -> Unload Project.

After unloading right click in the project and select "Edit {Project Name}.csproj"

Find the NuGet package entry within the XML where you previously uninstalled and delete it.

Install again the package from package manager console...

Antho
  • 181
  • 11
  • Uninstall was the key step I was missing. – HackSlash Jun 01 '18 at 21:17
  • I unloaded the project, and it worked for me. I appreciate your answer. Still, one would think Microsoft wouldn't leave you in a situation where you have to install what was uninstalled and can't uninstall it or install it! WOW! – JosephDoggie Oct 17 '22 at 19:13
3

None of the other answers worked for me. It kept going in an endless loop of install, revert, restart VS, never making any progress.

What I ended up having to do was:

  1. Close VS.
  2. Delete the offending packages from the packages folder.
  3. Edit the packages.config file manually, adding the package names and versions I needed to install.
  4. When I opened the solution again, I right-clicked the project and said "manage NuGet packages" and clicked the "Restore" button that appeared on the banner. This installed the packages that refused to install.

For crying out loud! Microsoft needs to take a lesson from yarn on how to do package management properly.

NH.
  • 2,240
  • 2
  • 23
  • 37
  • Note: the references were still not added properly, so installing them I guess did no good. – NH. Sep 20 '18 at 17:26
0

I discovered that the packages I installed were wrong / deprecated. When I went to remove them I got this error. None of the solutions worked for me here. I went into the project file and removed the packages in question and refreshed nuget and they were gone. I was then able to download the replacement nuget package.

Zonus
  • 2,313
  • 2
  • 26
  • 48