You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/consume-packages/Package-References-in-Project-Files.md
+81Lines changed: 81 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -142,6 +142,28 @@ Note that because `build` is not included with `PrivateAssets`, targets and prop
142
142
> [!NOTE]
143
143
> When `developmentDependency` is set to `true` in a `.nuspec` file, this marks a package as a development-only dependency, which prevents the package from being included as a dependency in other packages. With PackageReference *(NuGet 4.8+)*, this flag also means that it will exclude compile-time assets from compilation. For more information, see [DevelopmentDependency support for PackageReference](https://github.com/NuGet/Home/wiki/DevelopmentDependency-support-for-PackageReference).
144
144
145
+
## Targeting multiple frameworks
146
+
147
+
SDK-style projects support multi-targeting by listing multiple values in the `TargetFrameworks` property. When a project targets multiple frameworks, NuGet restore produces a separate dependency graph for each framework, and `dotnet pack` creates a package with framework-specific assets for each target.
For a step-by-step guide on setting up a multi-targeted project, see [Support multiple .NET frameworks in your project file](../create-packages/multiple-target-frameworks-project-file.md).
158
+
159
+
### How TargetFramework values work
160
+
161
+
The `TargetFramework` property in a project file is a friendly name — an alias — that gets translated into a canonical framework identity. The .NET SDK performs this translation by setting the `TargetFrameworkMoniker` (TFM), and when applicable, the `TargetPlatformMoniker` properties.
162
+
163
+
NuGet uses these moniker properties — not the `TargetFramework` string — for package compatibility checks. This means the `TargetFramework` value itself can be any string, as long as the moniker properties are set correctly.
164
+
165
+
For more details on the aliasing mechanism, see [TargetFramework values are aliases](../reference/target-frameworks.md#targetframework-values-are-aliases).
166
+
145
167
## Adding a PackageReference condition
146
168
147
169
You can use a condition to control whether a package is included. Conditions can use any MSBuild variable or a variable defined in the targets or props file. However, at present, only the `TargetFramework` variable is supported.
@@ -332,6 +354,9 @@ In order to persist the full closure of package dependencies, you can opt-in to
332
354
333
355
If this property is set, NuGet restore will generate a lock file (`packages.lock.json`) at the project root directory that lists all the package dependencies.
334
356
357
+
The `packages.lock.json` format is versioned.
358
+
The format version depends on the features you use, such as Central Package Management's transitive pinning or duplicate effective target frameworks.
359
+
335
360
> [!Note]
336
361
> Once a project has `packages.lock.json` file in its root directory, the lock file is always used with restore even if the property `RestorePackagesWithLockFile` is not set. So another way to opt-in to this feature is to create a dummy blank `packages.lock.json` file in the project's root directory.
337
362
@@ -471,6 +496,62 @@ You can leave off `$(AssetTargetFallback)` if you wish to overwrite, instead of
471
496
>
472
497
> `$(PackageTargetFallback)` was an earlier feature that attempted to address this challenge, but it is fundamentally broken and *should* not be used. To migrate from `$(PackageTargetFallback)` to `$(AssetTargetFallback)`, simply change the property name.
Because `TargetFramework` values are aliases, multiple aliases can resolve to the *same* effective framework. Starting with [NuGet 7.6](../release-notes/NuGet-7.6.md) / .NET SDK 10.0.300, NuGet and the .NET SDK support this scenario.
504
+
505
+
This enables use cases such as:
506
+
507
+
-**Multi-RID builds**: Build platform-specific assemblies from a single project.
A NuGet package can only contain one set of build output and one dependency group per effective framework. When you pack a project with duplicate effective frameworks, you must tell NuGet which alias contributes these assets or the pack raises [NU5051](../reference/errors-and-warnings/NU5051.md). See [NU5051](../reference/errors-and-warnings/NU5051.md) for resolution steps and examples.
544
+
545
+
#### Project references
546
+
547
+
When a project references another project that has multiple aliases resolving to the same framework, NuGet uses the alias name as a tiebreaker. If the referencing project has an alias with the same name as one in the referenced project, that alias is preferred. If there’s no matching name and multiple candidates exist, NuGet reports an error.
548
+
549
+
#### Limitations
550
+
551
+
- Only SDK-style projects support duplicate effective frameworks.
552
+
- Aliases that contain path separator characters (`/` or `\`) are blocked.
553
+
- Visual Studio’s Package Manager UI doesn’t have special support for duplicate frameworks, but you can manage packages by editing the project file directly or using the `dotnet` CLI.
554
+
474
555
## PrunePackageReference
475
556
476
557
The .NET Runtime is constantly evolving, with performance improvements and new APIs each release.
Copy file name to clipboardExpand all lines: docs/create-packages/multiple-target-frameworks-project-file.md
+10-6Lines changed: 10 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,22 +1,19 @@
1
1
---
2
2
title: Multi-targeting for NuGet Packages in your project file
3
-
description: Description of the various methods to target multiple .NET Framework versions from within a single NuGet package in your project file.
3
+
description: Description of the various methods to target multiple .NET frameworks from within a single NuGet package in your project file.
4
4
author: JonDouglas
5
5
ms.author: jodou
6
6
ms.date: 07/15/2019
7
7
ms.topic: how-to
8
8
---
9
9
10
-
# Support multiple .NET Framework versions in your project file
10
+
# Support multiple .NET frameworks in your project file
11
11
12
12
When you first create a project, we recommend you create a .NET Standard class library, as it provides compatibility with the widest range of consuming projects. By using .NET Standard, you add [cross-platform support](/dotnet/standard/library-guidance/cross-platform-targeting) to a .NET library by default. However, in some scenarios, you may also need to include code that targets a particular framework. This article shows you how to do that for [SDK-style](../resources/check-project-format.md) projects.
13
13
14
14
For SDK-style projects, you can configure support for multiple targets frameworks ([TFM](/dotnet/standard/frameworks)) in your project file, then use `dotnet pack` or `msbuild /t:pack` to create the package.
15
15
16
-
> [!NOTE]
17
-
> nuget.exe CLI does not support packing SDK-style projects, so you should only use `dotnet pack` or `msbuild /t:pack`. We recommend that you [include all the properties](../reference/msbuild-targets.md#pack-target) that are usually in the `.nuspec` file in the project file instead. To target multiple .NET Framework versions in a non-SDK-style project, see [Supporting multiple .NET Framework versions](supporting-multiple-target-frameworks.md).
18
-
19
-
## Create a project that supports multiple .NET Framework versions
16
+
## Create a project that supports multiple .NET frameworks
20
17
21
18
1. Create a new .NET Standard class library either in Visual Studio or use `dotnet new classlib`.
22
19
@@ -69,7 +66,14 @@ Here is the *.csproj* file that is generated using the preceding steps and .NET
69
66
</Project>
70
67
```
71
68
69
+
## Multi-targeting with duplicate frameworks
70
+
71
+
Starting with [NuGet 7.6](../release-notes/NuGet-7.6.md) / .NET SDK 10.0.300, you can use multiple `TargetFrameworks` values that resolve to the same underlying framework. This enables scenarios like building for multiple runtimes or targeting multiple versions of a host application from a single project.
72
+
73
+
For details on how this works with restore and pack, see [Targeting multiple frameworks](../consume-packages/package-references-in-project-files.md#multi-targeting-with-duplicate-frameworks).
74
+
72
75
## See also
73
76
74
77
*[How to specify target frameworks](/dotnet/standard/frameworks#how-to-specify-target-frameworks)
A project has multiple target framework aliases that resolve to the same effective framework, and pack can't determine which alias should contribute build output, dependencies, or framework references to the package.
17
+
18
+
### Issue
19
+
20
+
A project like the following has two aliases (`apple` and `banana`) that both resolve to `net10.0`:
When you run `dotnet pack`, NuGet raises NU5051 because it can't include duplicate build output or dependency groups for the same framework in a single package.
37
+
38
+
### Solution
39
+
40
+
Set `IncludeBuildOutput` to `false` and `SuppressDependenciesWhenPacking` to `true` on all but one alias per effective framework. This tells NuGet which alias contributes the build output and dependencies.
41
+
42
+
```xml
43
+
<!-- Let 'apple' contribute the build output and dependencies -->
If the aliases have different `FrameworkReference` items, use `PrivateAssets="all"` on the framework references in the secondary aliases to suppress them from the package.
51
+
52
+
## Scenario 2
53
+
54
+
A project has aliases for runtime-specific builds and wants to place each alias's build output into a custom package path (for example, `runtimes/<rid>/lib/<tfm>/`).
55
+
56
+
### Issue
57
+
58
+
A project like the following has `net10.0` as the primary alias and `linux` and `ios` as secondary aliases. All three resolve to the same effective framework:
Running `dotnet pack` raises NU5051 because three aliases produce build output and dependencies for the same framework.
75
+
76
+
### Solution
77
+
78
+
Suppress the default build output and dependencies for the secondary aliases, and use `TargetsForTfmSpecificContentInPackage` to place the secondary aliases' assemblies into custom package paths:
With this configuration, `net10.0` contributes the default `lib/net10.0/` build output and dependencies, while `linux` and `ios` place their assemblies under `runtimes/linux/lib/net10.0/` and `runtimes/ios/lib/net10.0/` respectively.
100
+
101
+
For more information about multi-targeting with duplicate frameworks, see [Targeting multiple frameworks](../../consume-packages/package-references-in-project-files.md#multi-targeting-with-duplicate-frameworks). For more information about pack extensibility, see [pack target](../msbuild-targets.md#pack-target).
0 commit comments