How can you find the unused NuGet packages in a so

2019-01-16 18:01发布

问题:

How can you find the unused NuGet packages in a solution?

I've got a number of solutions where there are a lot on installed packages, and a large number are flagged as having updates.

However I'm concerned there may be breaking changes, so I first want to clean up by removing any unused packages.

回答1:

ReSharper 2016.1 has a feature to remove unused NuGet.

It can be run on a solution and on each project in a solution and it does the following things:

  1. Analyze your code and collecting references to assemblies.
  2. Build NuGet usage graph based on usages of assemblies.
  3. Packages without content files, unused itself and without used dependencies are assumed as unused and suggested to remove.

Unfortunately, this doesn't work for project.json projects (RSRP-454515) and ASP.NET core projects (RSRP-459076)



回答2:

You can use the Visual Studio Extension ResolveUR - Resolve Unused References

Resolve unused references including nuget references in Visual Studio 2012/2013 projects via menu item at solution and project nodes Solution Explorer Tool Window.

Its not an easy task so i suggest to make a backup and/or commit before just in order to rollback if something went wrong.



回答3:

You can accomplish this using Resharper 2018.1.

Right click on the project > Refactor > Remove Unused References.

If your project is small you can also > Analyze Used References . . .

A window will pop up. Select all references and remove them all. Then go back and re-add the ones that give you a compiler error.



回答4:

This is manual labor, but it works.

  1. Use ReSharper or similar code analysis tool to identify any unused references in your projects and uninstall the nuget in the corresponding projects.

  2. Sometimes uninstalled nugets still linger in the Installed packages and Updates lists in the Manage NuGet Packages dialog. Close Visual Studio then delete the packages folder, then reopen the solution and restore your nugets.



回答5:

I don't think there is a default way to find this out. The primary reason being the variety of things these packages can do from referencing an assembly to injecting source code to your project. You may want to check the Nuget.Extensions though. The following thread on codeplex talks about an audit report of nuget packages.

http://nuget.codeplex.com/discussions/429694

(NuGet has been moved from Codeplex to GitHub. Archive of the above link:) https://web.archive.org/web/20171212202557/http://nuget.codeplex.com:80/discussions/429694