Sharing code with NuGet and .Net Standard

You've written some code and you want to share it around to all your microservices, which makes sense. So how are you going to do that, enter NuGet and .net standard.

Sharing code with NuGet and .Net Standard

You've written some code and you want to share it around to all your microservices, which makes sense. So how are you going to do that, enter NuGet and .Net Standard.

As a side note, per my post on metrics, I've created a shared metrics library for Prometheus metrics for a number of microservices, now I'm going to centralise that library and expose it on a private NuGet feed for all devs and services to pull that in.

1. NuGet Description

First you need to make sure you update the project description, version number or anything else you feel needs to be added as meta data in .csproj file for the package. You can do this by modifying the .csproj file directly or you can edit file in Properties and Package Tab of the project you want to build. The xml for the .csproj file might look something like this:

2. Pack the code

Second you need to pack it for publishing, so dive into a commandline for the project:

dotnet pack -c Release

3. Push your lib

Then from the bin/Release folder (make sure you are in the folder with the .nupkg file), you need to push it to the NuGet repo where ever that is located, if it's the public Nuget Repository you don't need the S switch here:

dotnet nuget push common.metrics.1.0.1.nupkg -s https://repo.mycode.com/artifactory/api/nuget/common

And if you take a look at the NuGet feed directly you should see your shiney new common libraries there for Visual Studio or NuGet consumers to use.

Simple!.

One small trick I have done is to put the new repo into the base docker images I use for my microservices, in that way I don't need to add a NuGet Config to each solution given that the location of the NuGet feed is alway in the builder docker image. In the builder Dockerfile you just need to COPY a NuGet.Config similar to the one below to the following path

/root/.nuget/NuGet/NuGet.Config

Depending on what Linux distro you are using this path might be slightly different, but this path works on Ubuntu 16.04.

Remember though that casing matters on Linux!

The file itself should look something like this:

Refs:

  1. https://docs.microsoft.com/en-us/nuget/quickstart/create-and-publish-a-package-using-the-dotnet-cli

  2. https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-pack?tabs=netcore2x