Can you target .NET Framework, .NET Standard and .NET Core in a NuGet package?

Viewed 2412

I'm new to creating NuGet packages and have only been playing around with them for a couple of days, as it now stands I've tested my package on .NET Frameworks 3.5 through 4.8 successfully.

By the way, this is the package: https://www.nuget.org/packages/ExcelFromList/1.0.4

My package relies on EPPlus, which is what I use as the engine. If you search for EPPlus you will see that it states that it can be installed on .NET Framework, .NET Standard, and .NET Core, I have tested this and it's correct.

So I'm not sure how to pull this off.

This is the info on my project:

  • Project is .NET 3.5
  • Using nuget.exe to create the NuGet package
  • Using .nuspec file for package creation (nuget pack <.nuspec file>)
  • Only NuGet package installed in project is EPPLus

This is my .nuspec file:

<?xml version="1.0" encoding="utf-8" ?>
<package>
  <metadata>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <id>ExcelFromList</id>
    <version>1.0.4</version>
    <title>ExcelFromList</title>
    <description>Straightforward and easy way to create stylized excel workbooks from lists. Add an image, title, subtitles and overal cell styles/formats. Uses the EPPlus engine.</description>
    <releaseNotes>Succesfully tested on .NET frameworks from 3.5 through 4.8</releaseNotes>
    <authors>Raul Marquez</authors>
    <owners>Raul Marquez</owners>
    <copyright>Copyright 2020 Raul Marquez</copyright>
    <projectUrl>https://github.com/RaulMarquezInclan/ExcelFromList</projectUrl>
    <license type="expression">GPL-3.0-only</license>
    <tags>excel list listtoexcel</tags>

    <dependencies>
      <dependency id="EPPlus" version="5.1.0" />
    </dependencies>

  </metadata>

  <files>
    <file src="bin\Debug\net35\ExcelFromList.dll" target="lib\net35" />
    <file src="bin\Debug\net35\ExcelFromList.xml" target="lib\net35" />
  </files>
</package>

Building the package with that configuration throws me this warning:

WARNING: NU5128: Some target frameworks declared in the dependencies group of the nuspec and the lib/ref folder do not have exact matches in the other location. Consult the list of actions below: - Add a dependency group for .NETFramework3.5 to the nuspec

However, as I stated before, it still installs and runs just fine throughout the different .NET Framework versions.

When attempting to install it on .NET Standard or .NET Core it will install my dll correctly but it won't install EPPlus, which is a dependency of my package. On the .NET Standard side, I only have to manually install it and all is good, on the .NET Core side after doing the same it tells me that the executable cant run in the project.

So, where I'm confused is on how to also make them available to .NET Standard and .NET core, from searching around I've gotten hints about converting it to a .NET standard in the first place, or having three different dlls each corresponding the the different technologies and specifying them in the files section of the .nuspec file, etc.

Can anyone please point me in the right direction?

EDIT: By the way, when looking at the EPPlus package this is what we see:

EPPLus

Thanks.

1 Answers

You can, if you upgrade your .csproj file to the "new" .csproj file format. I'd highly recommend you do that; the new project format is greatly simplified and easier to use.

In this new format, you can target multiple frameworks, like this:

  <PropertyGroup>
    <TargetFrameworks>netstandard2.1;netstandard2.0;net45;net40;net35</TargetFrameworks>
  </PropertyGroup>

The EPPlus project file is a great example of how to do multi-targetting.

You can then build your package by running dotnet pack, and it will automatically create a NuGet package which targets all of the versions of .NET listed in TargetFrameworks.

You will need to use the .NET Core SDK to build your project, but you can still run your package on desktop .NET.

Related