C# in 5 mins or less: Creating NuGet packages which target multiple frameworks

Vasu Gupta
2 min readJan 13, 2020

--

If you have a piece code that has the potential of being reused by other coders in your team, then creating a NuGet package will be a very useful and time-saving process. Creating NuGet packages will allow you to share code with your teammates resulting in saving them from countless hours of programming.

Step 1: Creating a .NETStandard Project

Start by creating an .NETStandard project which will contain the reusable code. Remember the code should be generic as possible using programming principles such as Generics. It's highly recommended that your project solution contains both tests and a sample CLI project which will allow anyone to check-out the project and understand the functionality of the project without a large amount of documentation.

Step 2 : Update .csproj of the Project

Next step is to update the .csproj file so that we target the different frameworks we want to. The .csproj file will also contain information regarding the NuGet package which is normally found in the .nuspec file.

For this example I will be targeting 3 different frameworks:

- .NETStandard2.0

- .NETFrameworks 4.6.2

-.NETCore 2.2

For further information regarding frameworks : https://docs.microsoft.com/en-us/dotnet/standard/frameworks#supported-target-framework-versions

Example : .csproj

<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFrameworks>netstandard2.0;net462;netcoreapp2.2</TargetFrameworks><files><file src="bin\Release\net462\INSERT-PACKAGE-NAME.dll" target="lib\net462\INSERT-PACKAGE-NAME.dll" /><file src="bin\Release\netstandard2.0\INSERT-PACKAGE-NAME.dll" target="lib\netstandard2.0\INSERT-PACKAGE-NAME.dll" /><file src="bin\Release\netcoreapp2.2\INSERT-PACKAGE-NAME.dll" target="lib\netcoreapp2.2\INSERT-PACKAGE-NAME.dll"/></files><GeneratePackageOnBuild>false</GeneratePackageOnBuild><PackageId>NUGET PACKAGE NAME</PackageId><Description>NUGET PACKAGE DESCRIPTION</Description><Version>0.0.0.1</Version><Authors></Authors><Company></Company><RepositoryUrl></RepositoryUrl><PackageTags>TAGS</PackageTags><NeutralLanguage>en-GB</NeutralLanguage><Copyright>Copyright 2020</Copyright><PackageReleaseNotes>INSERT ANY PACKAGE RELEASE NOTES</PackageReleaseNotes><PackageProjectUrl>INSERT REPO URL IN HERE</PackageProjectUrl></PropertyGroup></Project>

Step 3: Local Build

In order to build your project locally, you can either build the project using Visual Studio by selecting BUILD -> BUILD SOLUTION and then selecting BUILD -> PACK [Solution Name]. Or you can use the Visual Studio Developers CLI to build and pack the project into a NuGet package. Open Developer Command Prompt for VS 2019 by pressing window key and typing Developer Command Prompt for VS 2019. Once the CLI has started you can insert the following command to build and pack your project.

msbuild -t:pack -p:Configuration=Release -restore

If the local build is complete with 0 errors then inside ProjectFolder/Bin/Release you will find folders associated with the frameworks you specified in .csproj containing the complied DLL’s and the NuGet package.

You have now created your Nuget package which will contain DLL's for all the different frameworks which specified in our .csproj file.

You run additional scripts that can push the NuGet package to either to a NuGet server or to a ProGet server.

Refer to the links provided below:

https://docs.microsoft.com/en-us/nuget/nuget-org/publish-a-package

--

--