miércoles, 13 de marzo de 2013

Managing Visual Studio extensions with NuGet



NuGet is a Visual Studio extension that allows you to easily add third party libraries to a project. Net When you install a NuGet package files are automatically copied to the solution and automatically makes the necessary changes: add dependencies, modify the web.config. If you uninstall a package, NuGet "undo" those changes. NuGet is available from Visual Studio 2010.

nuget packages

Everything needed to install a package is contained in a. Nupkg. The package contains all the files to be copied to the project, a manifest file that describes the contents and the actions needed to install and uninstall the package.



The installation is done from the "Extension Manager" in Visual Studio (Tools menu> Extension Manager).




It may be that the extension is already installed. Then select Updates in the left panel. For some earlier versions of NuGet, it gives an error. If this is the case, you must uninstall the previous version and then install the new version. You should check for updates to the tool as new versions are released periodically .

Add, update and remove packages

add  a package to a project

On the project contextual menu, choose "Manage Nuget Packages"


On the left of the window, select the category "Online" to display the packages available for download. To the right is a text box that enables searches.




Once you find the searched package, select it. In the right pane displays the package information: creator, description, version, and dependencies. To install press "Install".



The tool downloads and installs as a dependency of the project the package with all the dependencies . If we now go to the category "Installed Packages" can see all the packages installed on the projects in the solution.








The tool will create the folder "packages" at the level of the solution, where the packages are stored


In addition, the project will add a file (packages.config) containing dependencies (package identifier and version) of our project. When you add a package to a project:

  • It downloads the package and dependent packages in folder 'packages' that is created at the level of the solution.
  • It Adds the necessary references to the project.
  • Where necessary, it modifies the configuration files.
  • Packages.config file is updated with the information project of the dependencies.

If we stand on the solution, and in the context menu select the option "Manage Packages for Solution", we can see the packages installed on the entire solution. To see which projects a package is installed, press the "Manage". Right there you can make other projects you want to install.




updating packages

The tool also allows you to upgrade previously installed packages to newer versions. To do this open the package management on the left and select the category "Updates". In the center pane displays all the packets for which there is a newer version available that is installed in the project. When you select a package, in the right pane displays the information that we have installed version and the new version. Clicking the "Update" button will update the package along with its dependencies.



removing packages

To remove a package, just go to the category "Installed Packages", select the appropriate package and press the "Uninstall".


No-Commit Strategy

One of the NuGet ‘s advantages to manage dependencies is that it allows not to commit / check-in third-party libraries to code repository. The option "Enable NuGet Package Restore" allows packets to be automatically downloaded to build the solution (if not previously downloaded). As the tool saves a configuration file in the exact versions of each component, you can download the same packages and versions that were originally added. To enable the restore, stand on the solution and from the context menu choose "Enable NuGet Package Restore".



A window will appear asking you to confirm the operation.



At the end we will see that in the solution was added the folder ".nuget" with the files necessary to perform the restore package. Important: the restore works even without Visual Studio. For example, if you run the build with msbuild from a continuous integration tool, packages are also downloaded automatically.

Tips & Tricks

Don't add ´manually dependencies NuGet downloaded libraries: If installing a library in one of the projects in the solution using NuGet and then we need to add it in another project in the same solution, use the "Manage Packages for Solution" . If we add the reference manually (using Add Reference or the tooltip of ReSharper), NuGet will not register this dependency and will not work properly (among other things the Restore Package). Only could be added manually those units that are not being administered with NuGet.

Nuget package restore: each developer has to enable it in Visual Studio: If another developer download the Restore Package solution, it may not automatically download the dependencies. In this case check in Visual Studio, on the menu "Tools> Options" the option "Allow NuGet to download missing packages During build" is enabled. This option is automatically enabled if we configure the restore in the solution, but can be disabled in the IDE from another developer who download the solution repository




Nuget Official Site.On the next link you can find +10000 packages , upload your own packages, query statistics , documents and a blog. http://nuget.org/



· Documents (Excellent documentation, including samples and videos): http://docs.nuget.org/


Thanks to Diego Tubello and Cristian Fernandez for creating this article.


Follow us on Twitter
Follow @baufest_ar
Follow @baufestusa