MongoDB C# Driver 2.13.1依赖System.ValueTuple致.NET 4.8构建失败如何解决
问题描述
目标框架为.NET Framework 4.8的项目,将Mongo C#驱动从2.6版本升级至2.13.1版本时遇到构建错误,已尝试显式添加System.ValueTuple 4.0.3版本引用、添加绑定重定向仍未解决,报错信息如下:
[msbuild] C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.targets(1605,5): warning MSB3258: The primary reference "MongoDB.Driver" could not be resolved because it has an indirect dependency on the .NET Framework assembly "System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" which has a higher version "4.0.3.0" than the version "4.0.2.0" in the current target framework. [C:\CODE***\MongoDB.csproj] [msbuild] MongoDBConnection.cs(4,15): error CS0234: The type or namespace name 'Driver' does not exist in the namespace 'MongoDB' (are you missing an assembly reference?)
解决方案
按照以下步骤依次排查即可解决:
- 清理旧引用与缓存:先卸载项目内所有MongoDB相关NuGet包(MongoDB.Driver、MongoDB.Bson、MongoDB.Driver.Core),再卸载System.ValueTuple的NuGet引用,删除项目目录下的
packages、bin、obj文件夹,清除本地NuGet缓存避免旧版本残留干扰。 - 按顺序重新安装依赖:先单独安装System.ValueTuple 4.0.3版本NuGet包,安装完成后再安装MongoDB.Driver 2.13.1版本,让NuGet自动匹配依赖关联,避免版本冲突。
- 校验绑定重定向配置:打开项目的
app.config(桌面项目)或web.config(Web项目),确认runtime节点下存在正确的绑定重定向规则,规则如下:
<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" /> </dependentAssembly> </assemblyBinding> </runtime>
- 调整引用属性:右键点击项目引用列表中的System.ValueTuple,选择属性,将
特定版本选项设置为False,关闭构建时的强制版本校验。 - 校验项目文件配置:右键卸载项目,编辑.csproj文件,找到System.ValueTuple的引用节点,确认引用的是NuGet安装的4.0.3版本,而非.NET Framework自带的4.0.2版本,正确配置参考:
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> <HintPath>..\packages\System.ValueTuple.4.0.3\lib\net472\System.ValueTuple.dll</HintPath> <Private>True</Private> </Reference>
- 开启自动绑定重定向:打开项目属性的「生成」页,勾选「自动绑定重定向」选项,完成后重新生成项目即可。
内容的提问来源于stack exchange,提问作者K2902
相关产品推荐
相关产品推荐

