Quantcast
Channel: Active questions tagged nuget-package - Stack Overflow
Viewing all articles
Browse latest Browse all 3067

How to restore assemblies from a NuGet package for a specific target framework only

$
0
0

I'm looking for a way to restore the assemblies for a NuGet package which targets exactly one framework, in this case net45.

This is my packages config:

<?xml version="1.0" encoding="utf-8"?><packages><package id="Newtonsoft.Json" version="12.0.1" targetFramework="net45" /></packages>

This is my NuGet config file:

<?xml version="1.0" encoding="utf-8"?><configuration><config><add key="repositoryPath" value=".\Nuget" /></config></configuration>

Version is: NuGet Version: 5.2.0.6090

Running: nuget restore packages.config -ConfigFile nuget.config restores the assemblies for all targetframework versions as can be seen by:

E:\Tmp\NuGet\Nuget\Newtonsoft.Json.12.0.1>dir lib Directory of E:\Tmp\NuGet\Nuget\Newtonsoft.Json.12.0.1\lib2019-09-30  18:27    <DIR>          .2019-09-30  18:27    <DIR>          ..2019-09-30  18:27    <DIR>          net202019-09-30  18:27    <DIR>          net352019-09-30  18:27    <DIR>          net402019-09-30  18:27    <DIR>          net452019-09-30  18:27    <DIR>          netstandard1.02019-09-30  18:27    <DIR>          netstandard1.32019-09-30  18:27    <DIR>          netstandard2.02019-09-30  18:27    <DIR>          portable-net40+sl5+win8+wp8+wpa812019-09-30  18:27    <DIR>          portable-net45+win8+wp8+wpa81

According to docs.microsoft.com

When NuGet installs a package that has multiple assembly versions, it tries to match the framework name of the assembly with the target framework of the project.

If a match is not found, NuGet copies the assembly for the highest version that is less than or equal to the project's target framework, if available. If no compatible assembly is found, NuGet returns an appropriate error message.

For example, consider the following folder structure in a package:

\net45  \MyAssembly.dll\net461  \MyAssembly.dll 

When installing this package in a project that targets .NET Framework 4.6, NuGet installs the assembly in the net45 folder, because that's the highest available version that's less than or equal to 4.6.

If the project targets .NET Framework 4.6.1, on the other hand, NuGet installs the assembly in the net461 folder.

From the paragraph above I understand that when I set the target framework I should be able to restore just the assemblies for that one target framework. In my case it looks like NuGet completely ignores the targetFramework attribute and always installs the dlls for all target frameworks. Changing it from net45 to net40 has no effect.

How can I make NuGet restore ONLY the dlls in a package for a specific target framework?


Viewing all articles
Browse latest Browse all 3067

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>