A P P E N D I X  A

Image

NuGet Introduction

NuGet is a Microsoft technology that makes it very easy to include, update, and distribute stand-alone libraries within your application. NuGet was first released a few years ago as an extension to Visual Studio 2010.

With Visual Studio 2012, NuGet is included out of the box and used extensively. For example, NuGet is used to retrieve the latest version of many project types' dependencies when projects are first created. NuGet is an excellent dependency management tool for your projects and can even include packages at build time.

NuGet Package Manager

There are a couple of ways to install NuGet packages. Probably the easiest method is using the graphical Package Manager. The package manager can be accessed in two ways. The first is from the main menu (Tools Image Library Package Manager Image Manage NuGet Packages for solution):

Image

Figure A-1. Accessing NuGet from Tools menu

The second is by right-clicking on the project or solution in Solution Explorer and selecting the Manage NuGet packages option from the context menu:

Image

Figure A-2. Accessing NuGet from Tools menu

It doesn't really matter which method you use, but I prefer to use the Solution Explorer method. There is also a more powerful command line interface that we will look at shortly.

Once you have opened the package manager, you should see a screen similar to Figure A-3 that allows you to install, uninstall, and update packages.

Image

Figure A-3. Accessing NuGet package manager

Notice how the NuGet package manager has a number of tabs to allow you to review installed packages, available updates, and any recently applied packages.

The other interesting option is that if you click the Settings button, you can configure additional package sources—this might be useful in enterprises or high-security environments where you want to review and control available packages as you can put these all on a network share.

Let's install the NUnit test framework from Package Manager. Select the online tab and then in the search box search for NUnit. Packages will be filtered to match those containing this term as shown in Figure A-4.

Image

Figure A-4. NUnit packages

To install the NUnit package, click the install button and NUnit will be installed—easy, eh? If you examine your solution, you will find NUnit assembly is now referenced and a new file called packages.config has been created that just contains information about the packages installed.

If you want to remove a package, simply go to the package manager console and click the Uninstall button and it will be uninstalled (see Figure A-5).

Image

Figure A-5. Uninstall option for NUnit package

Another option for installing packages is with the NuGet command line utility.

NuGet Command Line

The NuGet command line uses Powershell (v2) with some NuGet specific extensions (called cmdlets in Powershell world). It's much more powerful than the package manager window and is used when you create your very own packages.

Let's use the command line to install the xunit framework.

To bring up the NuGet command line console, go to Tools Image Library Package Manager Image Package Manager Console and you should see a screen similar to Figure A-6.

Image

Figure A-6. Package Manager console

There are a number of different NuGet command line commands. The following list contains some of the more commonly used commands:

  • get-package –ListAvailable (retrieves list of available packages—warning: there are a few!)
  • get-package -ListAvailable -Filter NUnit (returns packages with NUnit in title or description)
  • install-package nunit (installs NUnit package)
  • uninstall-package nunit (uninstalls NUnit package)
  • get-package –updates (gets any updates for installed packages within solution)
  • update-package nunit (updates NUnit package to latest version)

When you install a package, it will be installed in the default project and any dependent references will also be installed. You can configure where NuGet packages are installed with the –projectName option.

It can be useful to get information about how to use a command. To do this, simply type get-help before the command name.

For example, get-help install-package will show other options for this command and you should see an output similar to the following:

Install-Package [-Id] <string> [-IgnoreDependencies] [-ProjectName <string>] [[-Version]
<string>] [[-Source] <string>] [-IncludePrerelease] [<CommonParameters>]

Creating Your Own Packages

Creating your own NuGet packages is very easy. There are again two main ways to accomplish this. By far, the easiest method is using the NuGet Package explorer tool, which can be installed from http://docs.nuget.org/docs/creating-packages/using-a-gui-to-build-packages. (If you would rather do this via the command line, which is more powerful, please refer to http://docs.nuget.org/docs/creating-packages/creating-and-publishing-a-package.)

Once you have installed the NuGet package explorer, you should see a screen similar to Figure A-7.

Image

Figure A-7. NuGet Package Explorer

From this screen, you can specify your package contents by right-clicking on the right pane and selecting a context menu option. You can modify the descriptive properties of your package by selecting Edit Image Edit Package Meta data. Once you have created your package, simply select Save from the File menu. You can then install it by doubling-clicking on the Nupkg file or adding it to a local NuGet repository.

If you want to distribute your NuGet packages via Microsoft's repository, you must first register for an account at http://nuget.org/account/Register. This will give you an API key that you can then use when you publish your packages (under the File Image Publish menu).

In summary, NuGet makes it very easy to distribute and update packages within your application. You may wish to consider creating a NuGet package for commonly used libraries within your team or to distribute dependencies internally.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset