Nuget 不断将重定向恢复到不正确的版本,导致运行时错误

Nuget keeps reverting redirects to incorrect version, causing runtime errors

我有一个解决方案,其中使用了两个不同版本的包,将它们称为 "PackageA v1" 和 "PackageA v2"。

一个项目引用"PackageA v1" 15 个项目参考 "PackageA v2"

每次我做任何涉及 Nuget 的事情时,所有 15 个项目都会更新它们的 app.config 程序集绑定重定向以重定向到 v1 包。

之前:

<dependentAssembly>
        <assemblyIdentity name="PackageA" publicKeyToken="abc123" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>

之后:

<dependentAssembly>
    <assemblyIdentity name="PackageA" publicKeyToken="abc123" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-1.0.0.0" newVersion="1.0.0.0" />
</dependentAssembly>

即使最终出现在大多数项目的 bin 文件夹中的实际版本是 v2,也会发生这种情况!

我尝试从引用它的项目的 Nuget 包中删除 v1,认为如果我只是将 v1 的 dll 复制到包文件夹并手动添加引用,Nuget 将不会跟踪 v1 用于确定绑定重定向的任何内容.那没有用。

禁用自动程序集绑定重定向不是一个选项,因为该解决方案有大约 50 个项目,并且(除此之外)自动重定向在升级包时节省了大量时间。

即使像 "make Nuget choose the newest version rather than the oldest" 这样简单的东西也很棒。至少那样我只需要手动修复一个 app.config 而不是 15.

我希望有人有解决方案。提前致谢!

Nuget keeps reverting redirects to incorrect version, causing runtime errors

根据the bindingRedirect element on MSDN,恐怕你不能做这样的事情。

oldVersion: Required attribute.

Specifies the version of the assembly that was originally requested. The format of an assembly version number is major.minor.build.revision. Valid values for each part of this version number are 0 to 65535.

You can also specify a range of versions in the following format: n.n.n.n - n.n.n.n

newVersion: Required attribute. Specifies the version of the assembly to use instead of the originally requested version in the format: n.n.n.n

This value can specify an earlier version than oldVersion.

有关更多详细信息,请参阅 this thread

但是如果您想要“像 "make Nuget choose the newest version rather than the oldest" 这样简单的东西会很棒”,您可以将以下属性添加到您的 nuget.config(在路径,%appdata%\NuGet\NuGet.Config):

<configuration>
    <config> 
        <add key="dependencyversion" value="Highest" /> 
    </config>
</configuration>

解析您的包时,将解析该包的最新版本。