DOCX

introduction to ASPNET MVC NuGet packages

By Loretta Thomas,2014-09-20 15:15
7 views 0
introduction to ASPNET MVC NuGet packages

    Hands-On Lab ASP.NET MVC NuGet

Lab version: 1.1.0

    Last updated: 9/20/2011

    Page 1

CONTENTS

    OVERVIEW ............................................................................................................................................. 3

    EXERCISE 1: CREATING A NUGET PACKAGE .................................................................................... 4

    Task 1 Adding a nuspec File ........................................................................................................... 5

    Task 2 Creating the NuSpec Script ................................................................................................. 6

    Task 3 Generating the NuGet File .................................................................................................. 6

    Task 4 Using Post-Build Events to Generate the Package ............................................................... 7

    Task 5 Building and Packaging the Solution ................................................................................... 9 EXERCISE 2: CONSUMING NUGET PACKAGES ................................................................................ 10

    Task 1 Consuming an External Library from Visual Studio 2010 NuGet Extension......................... 11

    Task 2 Running the Solution ........................................................................................................ 13

    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.

In this Hands-on Lab, you will learn how to:

    ; Make a NuGet package for MvcMusicStore application

    ; Install NuGet Visual Extension tool in Visual Studio

    ; Update an ASP.NET MVC application with a package downloaded from local NuGet Server

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

    Exercise 1: Creating a NuGet Package In this exercise you will learn how to package your solution using NuGet.

    For that purpose, you will run a script that will create the MusicStore package in your project folder. The script will be configured with a nuspec (NuGet specification) file, which will specify all the package settings and dependencies.

    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>NuGetSampleid>

     <version>2.0.0version>

     <authors>Microsoftauthors>

     <description>This is an example of a nuspec filedescription>

     <tags>NuGet nuspec hands-on-lab microsoft asp.nettags>

     <language>en-USlanguage>

     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>

Task 1 Adding a nuspec File

    In this task, you will add a nuspec file that will contain a description of the package to be created.

    1. Open the Source\Ex01-Creating NuGet Package\Begin\MvcMusicStore folder in Windows

    Explorer.

    2. Create a new empty txt file and rename it to MvcMusicStore.nuspec.

    3. Open MvcMusicStore.nuspec in Visual Studio or any text/XML editor.

    4. Add the following XML specification to the MvcMusicStore.nuspec file:

    XML

    xml version="1.0" encoding="utf-8"?>

    <package>

     <metadata>

     <id>MvcMusicStoreid>

     <version>1.0version>

     <description>Music Store ASP.NET MVC Applicationdescription>

     <authors>Microsoftauthors>

     <language>en-USlanguage>

     metadata>

    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

    Note: Package file will be automatically named with nuspec attributes as

    ..nupkg

    The NuGet.exe command-line utility is a wrapper for the PackageBuilder API used to create

    packages. Therefore, as an alternative you can use the API directly to create packages.

Task 3 Generating the NuGet File

    1. Execute the CreateNuGetPackage.cmd file by double-clicking it. After that, check that a new

    package file MvcMusicStore.1.0.nupkg was generated in the same folder.

    2. Rename MvcMusicStore.1.0.nupkg as a .zip file.

    3. Open it with Windows Explorer to see its content. Note that the NuGet package contains the

    nuspec file used to build the package as well as the solution files.

    Page 6

    Figure 1

    .nuget file content

Task 4 Using Post-Build Events to Generate the Package

    In this task, you will use Visual Studio Post-Build events to automatically generate the NuGet package each time the solution is built. In order to do that, you will include the NuGet script into post-build events set up. Therefore, NuGet.exe will get executed in the same way as before, but you will not need an extra step each time you want to package:

    1. Open the begin solution at Source\Ex01-Creating NuGet Package\Begin\MvcMusicStore.

    2. Open MvcMusicStore project Properties from Solution Explorer:

    Page 7

    Figure 2

    Project properties

    3. Open the Build-Events tab and click on Edit Post-Build.

    Figure 3

    Adding Build Events

    4. Copy the following line into the Post-build Command Line dialog and click OK:

    Page 8

    Post-build Command

    "$(SolutionDir)..\..\..\Assets\NuGet.exe" pack

    "$(SolutionDir)MvcMusicStore.nuspec"

    Figure 4

    Creating a Post-build event

    Task 5 Building and Packaging the Solution In this task, you will build the solution and verify that the package is generated after the build.

    1. Build the solution or press CTRL+F5.

    2. Check that the package was generated at the bin folder:

    Page 9

    Figure 5

    Post-build NuGet generated at \bin folder

    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

    Exercise 2: Consuming NuGet Packages In this exercise, you will learn how to consume external libraries in your project by using NuGet public repositories. You will be able to add any packaged library from the cloud into your project. With that goal in mind, in this exercise you will add an ELMAH (Error Logging Modules and Handlers) Library to your project, enabling you to see the error log generated by the library. 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.

    Page 10

Report this document

For any questions or suggestions please email
cust-service@docsford.com