如何通过集中式NuGet版本管理仅引用x64版本包?
问题根源
chromiumembeddedframework.runtime是一个元包,它会根据项目目标平台自动引入对应架构的具体运行时包(如chromiumembeddedframework.runtime.win-x64、chromiumembeddedframework.runtime.win-x86、chromiumembeddedframework.runtime.win-arm64)。当开启集中式版本管理且CentralPackageTransitivePinningEnabled=true时,NuGet要求所有传递依赖的包版本必须在Directory.Packages.Props中通过PackageVersion声明,否则会触发NU1008错误——因为这些架构特定包的版本由元包自动带入,相当于在传递引用中定义了版本,违反了集中管理规则。
解决方案
方案1:声明所有架构特定包的版本(推荐)
在Directory.Packages.Props中,为所有可能被引入的CEF架构运行时包添加PackageVersion条目,版本与chromiumembeddedframework.runtime保持一致:
<?xml version="1.0" encoding="utf-8"?> <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally> <CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled> </PropertyGroup> <ItemGroup> <PackageVersion Include="CefSharp.Wpf.NETCore" Version="109.1.110" /> <PackageVersion Include="chromiumembeddedframework.runtime" Version="109.1.11" /> <!-- 添加所有架构特定的CEF运行时包版本声明 --> <PackageVersion Include="chromiumembeddedframework.runtime.win-x64" Version="109.1.11" /> <PackageVersion Include="chromiumembeddedframework.runtime.win-x86" Version="109.1.11" /> <PackageVersion Include="chromiumembeddedframework.runtime.win-arm64" Version="109.1.11" /> </ItemGroup> </Project>
方案2:限制项目目标平台为单一架构
如果解决方案仅需x64架构,可全局或在项目中固定目标平台,避免NuGet引入其他架构的包:
在项目文件Project.csproj中添加:
<PropertyGroup> <!-- 保留原有属性 --> <PlatformTarget>x64</PlatformTarget> <RuntimeIdentifier>win-x64</RuntimeIdentifier> </PropertyGroup>
或在Directory.Build.props中全局设置,让所有项目默认使用x64:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <PlatformTarget>x64</PlatformTarget> <RuntimeIdentifier>win-x64</RuntimeIdentifier> </PropertyGroup> </Project>
这种情况下,只需在Directory.Packages.Props中添加chromiumembeddedframework.runtime.win-x64的版本声明即可。
方案3:关闭传递依赖版本固定(不推荐)
如果无需对传递依赖版本进行集中控制,可关闭CentralPackageTransitivePinningEnabled:
<PropertyGroup> <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally> <CentralPackageTransitivePinningEnabled>false</CentralPackageTransitivePinningEnabled> </PropertyGroup>
此方法会降低版本管理的集中性,仅作为临时应急方案。
验证步骤
- 清理解决方案:执行
dotnet clean - 还原依赖:执行
dotnet restore - 重新编译:执行
dotnet build或在Visual Studio中重新生成
内容的提问来源于stack exchange,提问作者John Glover

