Create Nuget Package Visual Studio

broken image


Lately, we had a task in our team to create a NuGet package that would encapsulate logging, providing all the bits and pieces out of the box. We wanted it to be as easy to use as possibile, so the idea was to add default configuration to your appsettings.json on install. That’s something which used to be relatively simple with framework, where you could just transform Web.config to include library’s config sections. However, I got seriously stuck when trying to accomplish the same in dotnet core

  1. Create Nuget Package Visual Studio 2017 .net Framework
  2. Create Nuget Package Visual Studio 2017 Tutorial
  3. Create Nuget Package Visual Studio 2015
  4. Create Nuget Package Visual Studio
  5. Create Nuget Package Visual Studio Mac

I quickly realized I’m not quite up to date on how things work now with core and that I need to catch up a bit. As a result of this investigation, I wrote this summary to help my future self and others clear up the confusions. So, if you ever get stuck on creating NuGets, I hope you’ll find this article useful

To test the Nuget package, we can create a local feed of Nuget packages in Visual Studio. To create a local feed, create a new folder called MyLocalFeed on any drive. (You can name the folder as you like. I have created the folder on my E: drive.) Then add the Nuget package that was created earlier to this folder. An introduction to creating NuGet packages with Visual Studio and publishing to nuget.org. In this tutorial we will build a simple class library from scratch. Microsoft doc: explorer: https://github.com/NuGetPackageExplorer/Nu. This article explains the details of creating a NuGet package in a Visual Studio solution. Create a new project in VS and add the following two files: 1).targets file: when a release build is triggered in the VS solution. This file will get the nuspec file to create and push the NuGet package to the NuGet store. You can use multiple languages to write applications and libraries for.NET Core – C#, Visual Basic and F#. In order to create a NuGet package with Visual Studio 2019 – select new Class Library (.NET Core) project. I am going to call the project LoggingLib. In reality I’m not going to implement the code so it doesn’t matter the name.

So, in the “old days” of .NET Framework the very first thing you needed to do is to grab nuget.exe binary. For some reason, it’s not included in any version of Visual Studio. You can find it in the Downloads section of nuget.org.

Then, you need a nuspec file which is the manifest of your newly created package. It’s meant to describe the metadata, dependencies and content of a package. It could be bootstrapped with nuget spec command, which will create a scaffold of this file for you.

Example nuspec could look like this:

Having this file along with the sources, nuget pack command will create the package out of it. It’s probably worth noting that nupkg files are nothing more than plain old ZIP archives. Change the extension, unpack it, and you’ll be able to see what’s inside.

We can also push the package to a feed using nuget push command. Probably, you might also like to specify the URL and credentials when doing so:

I won’t cover there the details on how to get the ApiKey as it might vary depending on the feed you are using (Azure DevOps, Teamcity, self hosted, etc..).

Create Nuget Package Visual Studio

That’s really it when it comes to creating packages with framework. Basically, you call for a nuspec and there is no other way to create packages. Unfortunately, the process of creating nuspec file by hand can be very tedious and error prone.

Although there were GUI tools which eased the pain somehow, like open source NugetPackageExplorer, it was still hard, especially when it comes to dealing with dependencies. You had to specify all of them manually.. ouch..

Dotnet core to the rescue

Dotnet core eased the pain somehow, making the process of creating nuget packages a lot easier. First, Nuget is now part of dotnet SDK, and is available through dotnet nuget command. This is especially cool when you consider CI environments - no need to install additional executables anymore! Btw, you shouldn’t use nuget cli with dotnet core.

Second, dotnet cli supports generation of nuspec on the fly, based on the csproj. If you bring up properties window of a project targeting core, you’ll see a new Package section.

If you fill in this section, it will save these settings to the csproj and from that file we can then easily create nuget just by firing dotnet pack. It’ll translate all the dependencies and metadata set on csproj to a nuspec file and create the package. Actually, this way you don’t even see nuspec anymore.

Aand, we can make it even simpler by checking “Generate NuGet package on build” or adding following snippet to the csproj:

Create Nuget Package Visual Studio 2017 .net Framework

Then, nupkg will appear alongside binaries (binRelease) after each build. Neat, isn’t it?

Include json configuration in nuget package

Unfortunately, there are still some cases with dotnet core where you might want to fallback to barebone nuspec. For example, embedding content files into the package is not supported with csproj. By content files, I mean things like json files containing configuration snippets, e.g. logger settings.

Lucky for us, there is an option available in csproj allowing us to use custom nuspec files. That is the NuSpec node, which needs to be placed under PropertyGroup:

Then, we can define a nuspec which will copy json files to contentFiles directory. This is kind of special directory. If you place files within it, they’ll get copied to the root of the project using them. So, to get it working, you need to:

  1. Create contentFiles/any/any directory structure within the package solution.
    The any there means: any frameowrk, any cpu. You might constrain the files to particular framework/cpu architecture there if you wish
  2. Create contentFiles section in the nuspec which will include the files.
    All the paths there are relative to contentFiles directory
  3. Copy files to the budle using file node with target set to contentFiles

Have in mind though, that this way you need to maintain nuspec all by yourself, just like with .NET Framework. For example, if you add dependency to your package, like Newtonsoft.Json, you need to remember to include it in the nuspec.

Create Nuget Package Visual Studio 2017 Tutorial

By using NuspecFile in your csproj, all the things configured under Package tab will get ignored. Again, you need to maintain the whole manifest manually. Though, dotnet pack command and “Generate NuGet package on build” will work as before.

You might ask about Web.Config/App.Config transformations from .NET Framework times. In case you didn’t have the questionable pleasure of using them, they allow you to append/transform some xml nodes to existing project configuration.

It turned out, this feature is not supported at the time of writing with appsettings.json. As per the GitHub issue there are no plans to implement it either.

Trying it out locally

Before we finish, I wanted to show you one more neat trick. You can easily test your packages locally, without going through the CI pipeline or pushing them to the feed. The trick is to add the directory containing package(s) to nuget sources in Visual Studio.

Go to Tools->Options -> NuGet Package Manager -> Package Sources and add new source which will point to a directory lying somewhere on your disk. The packages will appear in the Manage NuGet packages window from now on.

Create Nuget Package Visual Studio 2015

Summary

Create Nuget Package Visual Studio

Dotnet core simplified sharing code through packages a lot. Fill in the “package” section in VS, and you’ll get nupkg generated on the build with very little effort.

Create Nuget Package Visual Studio Mac

However, this is not a silver bullet and has some notable limitations when compared with manually crafting a nuspec manifest. Lucky for us, we can still use the “down to the metal” approach when needed. I hope this guide helped you to understand what’s possible and what’s not, so you can make an informative decision in your team.





broken image