错误 NU1107: 检测到 'PackageA' 的版本冲突。引用包: PackageB 2.0.0 -> PackageA (>= 1.5.0), PackageC 3.0.0 -> PackageA (>= 2.0.0)。
error NU1107: Version conflict detected for 'PackageA'. Reference packages: PackageB 2.0.0 -> PackageA (>= 1.5.0), PackageC 3.0.0 -> PackageA (>= 2.0.0).
ID: dotnet/nuget-package-version-conflict
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 5.0 | active | — | — | — |
| 6.0 | active | — | — | — |
| 7.0 | active | — | — | — |
| 8.0 | active | — | — | — |
| 9.0 | active | — | — | — |
根因分析
NuGet 包版本冲突发生在依赖关系图中不同包要求相同包的不兼容版本时,导致构建失败。
English
NuGet package version conflict occurs when different packages in the dependency graph require incompatible versions of the same package, causing the build to fail.
官方文档
https://learn.microsoft.com/en-us/nuget/reference/errors-and-warnings/nu1107解决方案
-
在项目文件中添加绑定重定向或使用特定版本的 PackageReference。示例:<PackageReference Include="PackageA" Version="2.0.0" /> 并确保所有依赖包更新到兼容版本。
-
使用 'dotnet nuget why' 命令分析依赖树。示例:dotnet nuget why PackageA
-
将所有包更新到最新的兼容版本:dotnet list package --outdated 然后 dotnet update package
无效尝试
常见但无效的做法:
-
Manually editing the .csproj file to force a specific version of PackageA without considering transitive dependencies
65% 失败
Forcing a version may break other packages that depend on a different version, leading to runtime errors or further conflicts.
-
Removing one of the conflicting packages entirely from the project
50% 失败
Removing a package may remove required functionality, causing build errors or missing method exceptions.
-
Running 'dotnet restore --force' without resolving the version mismatch
80% 失败
Restore only re-downloads packages; it does not resolve version conflicts unless package versions are explicitly aligned.