Introduction to NuGet

Try as it might, Microsoft cannot provide every possible piece of code a developer could need. There are millions of developers on the .NET platform, each with unique problems to solve. It doesn't scale or make sense to wait on Microsoft to solve every problem.

The good news is that many developers don't wait around to “scratch their own itch.” They solve their own problems (and those of their peers) with useful libraries that they write and then distribute on the Web.

Three big challenges with all these libraries out there in the wild are discovery, installation, and maintenance. How do developers find a library in the first place? Once they find it, how do they make use of it in their projects? And once they've installed it, how do they track project updates?

This section walks through a quick example of the steps necessary to install ELMAH without the benefit of NuGet. ELMAH stands for Error Logging Module and Handler and is used to log and display unhandled exception information within a web application. The steps are especially familiar to the NuGet team because we use ELMAH in the NuGet.org site, which is discussed in Chapter 16.

These are the steps it takes to make use of the library:

1. Find ELMAH. Due to its unique name, this is easy with any search engine.
2. Download the correct zip package. Multiple zip files are presented and, as I personally learned, choosing the correct one isn't always trivial.
3. “Unblock” the package. Files downloaded from the Web are marked with metadata that specifies they came from the “web zone” and are potentially unsafe. This mark is sometimes referred to as the “Mark of the Web” (MOTW). It's important to unblock the zip file before you expand it, otherwise every file within has the bit set and your code won't work in certain cases. If you're curious about how this mark is set, read up on the Attachment Manager in Windows, which is responsible for protecting the OS from potentially unsafe attachments (http://support.microsoft.com/kb/883260).
4. Verify its hash against the one provided by the hosting environment. You do verify the hash of the file with the one listed in the download page to ensure that it hasn't been altered, don't you? Don't you?!
5. Unzip the package contents into a known location. Typically, this will be placed in a lib folder so you can reference the assembly. Developers typically don't want to add assemblies directly to the bin directory because they don't want to add the bin directory to source control.
6. Add an assembly reference. Add a reference to the assembly in the Visual Studio Project.
7. Update web.config. ELMAH requires a bit of configuration. Typically, you would search the documentation to find the correct settings.

All these steps for a library, ELMAH, that doesn't even have any dependencies! And if the library does have dependencies, every time you update the library, you need to find the correct version of each dependency and repeat each of the previous steps for each dependency. This is a painful set of tasks to undertake every time you are ready to deploy a new version of your application, which is why many teams just stick with old versions of their dependencies for a long time.

This is the pain that NuGet solves. NuGet automates all these common and tedious tasks for a package as well as its dependencies. It removes most of the challenges of incorporating a third-party open source library into a project's source tree. Of course, it is still up to the developer to use that library properly.

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

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