gpt4 book ai didi

c# - update-package 运行时约束 PackageReference 升级版本

转载 作者:行者123 更新时间:2023-11-30 12:20:52 25 4
gpt4 key购买 nike

在 .NET 较旧的 NuGet packages.config 系统下,I could constrain the possible versions of a package在使用 allowedVersions 更新软件包时考虑Package 元素上的属性

<package id="Newtonsoft.Json" version="10.0.3" allowedVersions="[10.0.3]" />

update-package在 Visual Studio 中针对包含上述内容的项目运行,Newtonsoft.Json 不会发生更新,因为我已使用 allowedVersions 属性固定到 10.0.3。

我如何在 PackageReference 下实现这一点? ?正在申请 semver Version 属性的语法只影响版本恢复 - 它不限制更新。因此,如果我指定以下 PackageReference 并运行 update-package,例如,如果 11.0.1 在我的 NuGet 存储库中,我将升级到 11.0.1。

<PackageReference Include="Newtonsoft.Json" Version="[10.0.3]" />

背景

我们依靠命令行工具来更新包,因为我们既有快速移动的内部包(每天更新多次),也有更稳定的低移动包(例如:ASP.NET)。在大型代码库上,手动更新 .csproj 文件中的每个依赖项对我们来说根本不可扩展(并且容易出错)。在 packages.config 下,我们可以“固定”我们不想升级的第三方包,也可以更新到最新的快速移动依赖项。

最佳答案

来自 this answer :

At the moment, this is not possible. See this GitHub issue for tracking.

The cli commands for adding references however support updating single packages in a project by re-running dotnet add package The.Package.Id.

来自 GitHub Issue 4358 :

There is no PackageReference replacement for update yet, the command to modify references is only in dotnet.

您可能想参与开放功能请求 GitHub issue 4103关于这个(4358 作为重复关闭)。 Microsoft 并未将此功能放在高优先级(它最初于 2016 年 10 月开放)。

可能的解决方法

选项1

可以通过删除和添加引用来“更新”依赖项。根据this post ,使用命令明确指定版本将安装准确版本,而不是最新版本。我还确认您可以使用以下命令添加版本限制:

dotnet remove NewCsproj.csproj package Newtonsoft.Json
dotnet add NewCsproj.csproj package Newtonsoft.Json -v [10.0.3]

您可以使用这些命令做什么:

  1. 将包的版本号保存在一个文本文件中(也许只是将其命名为 packages.config )。
  2. 使用脚本创建您自己的“更新”命令,该命令读取文本文件并使用上述 2 个命令循环处理每个依赖项。该脚本可以设置为传递 .sln文件来处理其中的每个项目。

选项 2

使用 MSBuild 从通用 MSBuild 文件“导入”依赖项,您可以在一个地方更新版本。

您可以定义自己的<IncludeDependencies>元素以包含每个项目的特定依赖项。

SomeProject.csproj

<Project Sdk="Microsoft.NET.Sdk">

<IncludeDependencies>Newtonsoft.Json;FastMoving</IncludeDependencies>
<Import Project="..\..\..\Dependencies.proj" />

...

</Project>

Dependencies.proj

<Project>

<ItemGroup>
<PackageReference Condition="$(IncludeDependencies.Contains('Newtonsoft.Json'))" Include="Newtonsoft.Json" Version="[10.0.3]" />
<PackageReference Condition="$(IncludeDependencies.Contains('FastMoving'))" Include="FastMoving" Version="3.332.0" />
</ItemGroup>

</Project>

关于c# - update-package 运行时约束 PackageReference 升级版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49398602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com