An Introduction to NuGet

by admin 9. May 2012 17:09

NuGet Package ManagerGetting started with NuGet can be very confusing because there are really four parts to it with separate downloads. This post is an effort to make it easier to grasp and get started.

NuGet is one of those tools that you may not notice at first, but once you start using it you'll wonder how you ever got by without it. Not just a package manager, it is an ecosystem, a foundation upon which many different things can be built. A NuGet package can consist of assemblies, source code, config files, images, scripts, etc.

Do yourself a big favor and grab a copy of Pro NuGet by Maarten Balliauw and Xavier Decoster. From the book: "NuGet brings you all the benefits of any package manager: it takes care of dependency chains and versioning conflicts at installation time, and it facilitates finding, installing, updating and uninstalling packages on application level. Besides these common must-have functionalities, NuGet has much more to offer."

NuGet relieves us of the following headaches when trying to incorporate a third party library into our project:

1) Locate the application

2) Download the correct version of the package

3) "Unblock" the package

4) Unzip the files into our solution

5) Add an assembly reference

6) Update Web.Config with the correct entries

With NuGet, this is all done for us with one or two clicks!

Tooling

There are four main tools I use with NuGet, three by Microsoft and a third party tool called Package Explorer, created by Luan Nguyen, which I consider indispensable for routine everyday tasks.

 

Package Manager Console Microsoft
Use WPI*
Package Manager GUI Microsoft
Use WPI*
NuGet Command Line Tool Microsoft Download
Package Explorer Third Party Tool Download

* Web Platform Installer

 

1) Package Manager Console The Package Manager Console is a Visual Studio Extension that allows us to interact with NuGet from within Visual Studio using cmdlets ("commandlets") for routine operations.

 

Package Manager Console

 

2) Package Manager GUI The Package Manager GUI is a graphical user interface which allows us to visualize and edit package contents and metadata.

 

Package Manager GUI

 

3) NuGet Command Line Tool Interact with NuGet from a simple console window. Also used by CI systems and deployment environments to get or publish packages.

 

NuGet Command Line


4) Package Explorer NuGet Package Explorer is a ClickOnce application which allows creating and exploring NuGet packages easily. After installing it, you can double click on a .nupkg file to view the package content.

 

Package Explorer

More Information




Maybe it's just me but it really annoys me that files are opened in the tab bar in Visual Studio by default to the left. I naturally tend to think from left to right as that is the way I read.

In order to change this behavior so that the tabs open to the right of any currently open tabs, go to the Tools drop down menu in Visual Studio and configure as follows:

  • Tools -> Options
  • Environment -> Documents
  • Check the "Insert documents to the right of existing tabs" checkbox

Problem solved :-)

 

Visual Studio Tab Options



Guest Book Solution You can also view part I of this article which describes the setting up of this application with Visual Studio 2008 and IIS 7, running on Vista. SQL Server 2005 was used as the database. I had originally blogged on this using VS 2005 but feedback indicated that quite a few people are now using VS 2008, so I upgraded it to VS 2008, which I am currently running on VMWare Workstation... the best $190 I ever spent. This is part II and the full code will be available for download in part III, real soon!

Speaking of feedback, I have been having problems with the comments system on this blog for some time now. So, pretty soon I am upgrading the blog engine and switching to a database version. The plan is to eventually make it a real "Community" site. Watch this space...

The Guest Book application is written in C# and is pretty straightforward. Here I will walk through the creation of the back-end which will allow the site administrator to moderate the comments posted to the guest book by site visitors.

First, we need to set up our membership system. The membership system is where most people seem to get stuck. There are several ways of achieving the same thing and I think that this is what causes much of the confusion. Typically, in an application such as this you would use a CAPTCHA control to combat the spammers, so we are not going to force users to register in order to sign the guest book. We will need an administration area for our admin forms, so we will create an "admin" role. The way I go about this is to create a new folder and call it Admin. Add a config file  to this new folder and set up the authorization rules as shown below.

Admin Web.config

Now, we're going to cheat a little. I'm going to do the TV chef and slip in a little something I pre-prepared earlier in the kitchen (actually gleaned from several of Scott Guthrie's blog postings). I always keep a sample Web.config file handy that I know is set up correctly for a basic membership system. I start this way rather than jumping in right off with the WSAT wizard because I want to have one single database for both my application and membership data. Tidier. So, I manually set up the database connection in the project Web.config file, along with entries for the membership and authentication sections. See the snippets below, and don't forget to include the roleManager tag!

Project Web.config

Now that the membership database connection is configured the way we want it, we can run the aspnet_regsql tool from the command line. This will create the actual membership database objects for us as part of our Guests database. Just navigate from the start menu to SQL Server Management Studio and locate the command line tool under the Configuration Tools folder. Simply enter aspnet_regsql at the prompt and you will be presented with the SQL Server Setup Wizard. Here are some screenshots showing the correct selections to make:

SQL Server Setup Wizard

SQL Server Setup Wizard

SQL Server Setup Wizard

If you go to SSMS and refresh the database, you should now be able to expand the tables icon, and you will see that the wizard had created everything we need for our membership system. At this stage, you are ready to run the WSAT tool and start setting up the admin role and adding the admin user. I will outline this process in the final installment, as well as the creation of our administration forms. Stay tuned!

Membership Database Objects

kick it on DotNetKicks.com   PHP, ASP, .NET, JSP Resources, Reviews