Advertisment

Implementation Guide: Using Open Source Libraries in .NET Packages 

author-image
PCQ Bureau
New Update

Developing in .NET has always been an easy and productive way of creating applications — unless trying to use or contribute to open source. Using an open source .NET package has usually meant getting the project, compiling it, making the right references in the .config files and then hoping that the rest of your apps run with it. Contributing to .NET open source required you to put it up on CodePlex and give instructions on how to use it in different types of .NET projects. In both cases, open source projects are more developer-unfriendly than friendly.

Advertisment

This is where a new tool called NuGet comes in. Developed by Microsoft for Visual Studio 2010 (including the free Express editions), it allows developers to not only easily find and use open source libraries within their own projects, but also allows them to contribute their code for others to use within their projects.

NuGet itself is free and open source as well.

Adding OSS libraries

Advertisment

There are many ways that you can install NuGet for VS2010. If you download the latest ASP.NET MVC3 stack from www.asp.net, NuGet 1.0 is bundled within. If you want to get it directly, head over to www.nuget.org and download and install it. Or open up Visual Studio 2010's Extensions Manager (inside the Tools menu) and look for NuGet within it. Once NuGet is installed, VS2010 will also automatically look for updates if available. NuGet is currently available as version 1.1 as of this writing.

Advertisment

Once NuGet is installed, there are many different ways you can add open source libraries to your current project. The first is through the References section of your project. Simply right-click the References folder and choose the new Add Library Package Reference.... This brings up a dialog box that downloads a “package list” from the NuGet repository. There is a default 'official' NuGet repository — but you can add your own ones if you want as well. You can also search for any package within the repository using the name or any keyword. For instance, if you want to add PDF creation or Twitter features in your .NET project, simply enter 'PDF' or 'Twitter' in the search box and you will be presented with open source libraries for .NET that enable such development. Click the Install button to download and install the package.

Installing a package requires first agreeing to whatever OSS license it subscribes to. Once that it done, NuGet will download and install the binaries in the references as well as make whatever changes are required in the app's config files — be it a Windows app (WinForm, WPF) or Web app (WebForm, MVC, SL, etc.). You can also check for any updates to the installed open source packages in your project.

Click on the images to enlarge

Advertisment

Advertisment

The other way to install a package is to use the Package Manager console within VS2010. This gives you a lot more power in how you install these libraries. Open the console from the Tools | Library Package Manager menu in VS2010. This brings up a PowerShell console from which you can query, find, install land otherwise manage the NuGet libraries. To get a list of commands available simply type “get-help NuGet” in the console.

To get a list of packages from the repository, type: Get-Package —remote. To find a package with a keyword, enter: Get-Package —remote —filter Twitter. To install a particular package, enter: Install-Package . This will download and install the package and any dependencies to the current project as well as making any changes required in the config files.

The really nice part about NuGet is that in case you wish to remove a library, it will cleanly remove everything — config file entries, other dependent libraries etc.

Advertisment

You can also update an installed package to a newer version or install a particular version if you wish to do that.

Contributing NuGet packages

The other thing that NuGet allows you to do is create and distribute your own libraries that can be used for other projects. This involves creating a folder structure that contains the items that you wish to distribute — source, assemblies, content, and transformations (for config file changes).

Advertisment

Once these are created, you need to create a NuGet manifest file — basically an XML file that defines the properties of the package — such as the name, version, developer, help etc. This manifest file is passed to NuGet which finally creates the package as a .nupkg file. Once you do have such a package there are two things you can do. You can contribute this package to the official NuGet library at www.nuget.org. Create an account there and upload the package. Once it is approved, you (and everyone else using NuGet) will start seeing it in the repository list.

The other thing that you can do is host an enterprise repository. Simply create a virtual directory in IIS on a server and dump all the .nupkg files you want there. Go to the settings screen of the Package Manager in VS2010 and add the URL to this virtual folder. You can now choose which repository VS2010 uses to get the packages as well.

NuGet is a great tool from Microsoft to enable open source development in .NET — both for using as well as for contributing. Install it today and see the stuff that you can do with it.

Advertisment