My Journey to developing and publishing my first Nuget Package

[Since the release of this blogpost, Microsoft changed a lot in .Net Core, which makes it no longer relevant]

A few days ago I had an idea about a project, Pather.CSharp. Since libraries in .NET are managed using Nuget, I wanted to publish Pather.CSharp as Nuget Package.

Microsoft revealed a new form of application development with UWP (Universal Windows Platform) and Asp.Net 5, which focuses a lot more on Nuget Packages as dependency.

Setup

After installing Asp.Net 5, a few new project templates are available in Visual Studio, including the one I’m using for Pather.CSharp, namely Class Library (Package). It is located in > Templates -> Visual C# -> Web

Testing

Testing is a very important topic in modern software development. Unfortunately, these dnx projects are not as easy to test as “normal” projects. This has multiple reasons:

  1. They don’t play nicely with other (non-dnx) projects
  2. The standard Microsoft unit test framework does not support dnx projects

These two (different) project types cannot be added to each other as project dependency. The only way to combine them is by adding the dll. This has the huge disadvantage that Visual Studio doesn’t recompile dependent projects if one changes.

For these reasons the Unit Test project cannot be used to unit test dnx projects.

Solution

The easiest solution is to simply use XUnit as testing framework. It has a great guide on how to test dnx projects.

The gist of it is:

  1. Create a Class Library (Package) project
  2. Add dependencies to xunit and its test runner, xunit.runner.dnx in the project.json file
"dependencies": {
    "xunit": "2.1.0",
    "xunit.runner.dnx": "2.1.0-rc1-build204"
},

It also seems to be the standard for now, since it is the only example in the Asp.Net 5 documentation.

Publishing

Getting the compiled files

Compiling a dnx project does not, by default, create files for the compiled project.
To get those files, it is necessary to check Produce outputs on build in > Project Properties -> Build

Then they are saved in

SolutionFolder/artifacts/ProjectName/Release|Debug/

Publish to nuget.org

Before actually uploading to nuget.org, the package information (like version number, author) has to be updated. This is done in the project.json file.

Before the Class Library (Package) project template was available, it was necessary to package the resulting dll into a nuget package.
Fortunately, Visual Studio now does all steps except Publishing in NuGet Gallery for us.

It is possible to take the resulting .nupkg file and simply upload it to nuget.org.

Project specific Problems

After I first uploaded my package to nuget.org and then tried to install it, I got an error saying > Unable to find a version of ‘System.Text.RegularExpressions’ that is compatible with ‘Pather.CSharp 0.1.0 constraint: System.Text.RegularExpressions (>= 4.0.11-beta-23516)‘.

Error

I’m not quite sure what causes this error, but the solution was to change the dependency from the beta version to the latest major version.

"dependencies": {
    "System.Text.RegularExpressions": "4.0.0"
}