Hands-On Lab: Lab Version: 1.1.0 Last Updated: 6/27/2020
Hands-On Lab: Lab Version: 1.1.0 Last Updated: 6/27/2020
Contents
Page 1
OVERVIEW................................................................................................................................................. 3
SUMMARY................................................................................................................................................ 14
Page 2
Overview
Note: This Hands-on Lab assumes you have basic knowledge of ASP.NET. If you have not used ASP.NET
MVC before, we recommend you to go over ASP.NET MVC Fundamentals Hand-on Lab.
With .NET you can develop class libraries, web projects, complete solutions, frameworks, application
servers, web controls and helpers, etc. A live ecosystem of such packages (many of them open source
projects) is available for you to download from the Internet and add to your own solutions. You can also
create your own library and publish it. Nevertheless, this model implies some costs: you should locate
the appropriate software, download and compile it, resolve dependencies of other packages, etc.
With NuGet (http://nuget.codeplex.com) tool, you can download, create, and publish software
packages, ready to be used by other developers, resolving the hassles of dependencies tracking. You can
use a console tool to create a new package, upload it to a Microsoft server or to a server of your own,
browse and install them from Visual Studio.
System Requirements
You must have the following items to complete this lab:
ASP.NET and ASP.NET MVC 3
Visual Studio 2010
SQL Server Database (Express edition or above)
Exercises
This Hands-On Lab is comprised by the following exercises:
1. Exercise 1: Creating a NuGet Package
2. Exercise 2: Consuming NuGet Packages
Page 3
Estimated time to complete this lab: 30 minutes.
Note: Each exercise is accompanied by an End folder containing the resulting solution you should
obtain after completing the exercises. You can use this solution as a guide if you need additional help
working through the exercises.
Next Step
Exercise 1: Creating a NuGet Package
Note: A nuspec file is a manifest in XML format that is used to build the package and is also stored in
the package after the build process. It contains:
- The metadata for a package that includes the following fields: Id, version, authors (collection of
author elements), description, language, tags (collection), licenceUrl (Uri), projectURL (Uri), iconUrl
(Uri) and requrireLicenceAcceptance(boolean).
- An optional list of package’s dependencies. Each dependency element has the following attributes:
id (the ID of a package that this package depends on), version (the required version of the dependency
package. If you enter this value as a specific version number, such as 1.2.3, NuGet assumes that any
package of that version or later is allowable. You can be more specific about which versions are
allowable by using interval notation; for more information, see Specifying Version Ranges).
- An optional list of files to include in the package. Each file element has the following attributes:
src, target (both are paths).
Other considerations:
- If no version is specified, NuGet will assume that the latest version is being used.
- All paths are relative to the nuspec file, unless an absolute file is specified.
For more information, check http://nuget.codeplex.com/documentation?title=Nuspec Format.
Page 4
Below is a sample nuspec file that uses most of the described elements:
XML
<?xml version="1.0" encoding="utf-8"?>
<package>
<metadata>
<id>NuGetSample</id>
<version>2.0.0</version>
<authors>Microsoft</authors>
<description>This is an example of a nuspec file</description>
<tags>NuGet nuspec hands-on-lab microsoft asp.net</tags>
<language>en-US</language>
</metadata>
<dependencies>
<dependency id="sample-package" version="3.0.0" />
<dependency id="another-package" version="4.0.0" />
</dependencies>
<files src="..\sample\anyfile.dll"/>
<files src="..\sample\requiredLibrary.dll" target="bin"/>
</package>
Note: Language attribute is an ISO code. You could find more information about
interntionalization in this article at msdn.
Page 5
Task 2 – Creating the NuSpec Script
In this task, you will create a script that will automatically use NuGet executable with
MvcMusicStore.nuspec specifications:
1. Open the Source\Ex01-Creating NuGet Package\Begin folder in Windows Explorer.
2. Create a new txt file and rename it to CreateNuGetPackage.cmd.
3. Open CreateNuGetPackage.cmd in Visual Studio or any text editor, and add the following
lines:
cmd
@echo off
..\..\Assets\NuGet.exe pack MvcMusicStore\MvcMusicStore.nuspec
Page 6
Figure 1
.nuget file content
Page 7
Figure 2
Project properties
Figure 3
Adding Build Events – C#
Page 8
Figure 4
Adding Build Events – VB
4. Copy the following line into the Post-build Command Line dialog and click OK:
Post-build Command
"$(SolutionDir)..\..\..\Assets\NuGet.exe" pack "$
(SolutionDir)MvcMusicStore.nuspec"
Page 9
Figure 5
Creating a Post-build event
Figure 6
Post-build NuGet generated at \bin folder
Page 10
3. Rename MvcMusicStore.nupkg as a .zip file.
4. Open it with Windows Explorer to see the content. Note that each nuget package contains
the nuspec file used to build the package.
Next Step
Exercise 2: Consuming NuGet Packages
Note: ELMAH (Error Logging Modules and Handlers) is a pluggable library for error and exception
logging that can be dynamically added to an ASP.NET web application.
To read more about ELMAH, see this article at msdn.
Task 1 – Consuming an External Library from Visual Studio 2010 NuGet Extension
In this task, you will include ELMAH library into your project to add an error log.
1. Open the begin solution at Source\Ex02-Consuming and Packing
NuGet\Begin\MvcMusicStore.
2. From Solution Explorer, open References folder and right-click to add Library Package
Reference.
Page 11
Figure 7
NuGet Tool – Adding a package reference
Figure 8
NuGet Tool – Installing a package reference
Page 12
Figure 9
Package Manager Options – Elmah Library added to the solution
Figure 10
Project folder– Elmah Library download location
Page 13
2. Browse an inexistent URL like /test. You should see a “The resource cannot be found” error
message.
3. Browse /elmah.axd to see the log that will include a line for the error logged when browsing
an inexistent URL:
Figure 11
Elmah Library log view
Next Step
Summary
Summary
By completing this Hands-On Lab you have learned how to use NuGet Packages in your solution:
Create a NuGet package for your application
Add a Package Reference from Visual Studio
Page 14