.NET Framework 4.8中Google API包升级引发System.ValueTuple版本冲突
.NET Framework 4.8中Scriban与Google API包升级后的System.ValueTuple加载异常
环境信息
- 操作系统:Windows 10 64位 / Windows Server
- .NET版本:.NET Framework 4.8
- 开发工具:Visual Studio 2022,基于packages.config的项目
复现步骤
- 添加Scriban 6.2.1包
- 添加并升级Google.Api.Gax等相关包至4.11.0/2.17.0版本
- 运行调用Scriban的代码
报错详情
System.TypeInitializationException: The type initializer for 'Scriban.Functions.BuiltinFunctions' threw an exception. ---> System.TypeInitializationException: The type initializer for 'Scriban.Functions.DateTimeFunctions' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.ValueTuple, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified. at Scriban.Functions.DateTimeFunctions..cctor() --- End of inner exception stack trace --- at Scriban.Functions.BuiltinFunctions.DefaultBuiltins..ctor() at Scriban.Functions.BuiltinFunctions..cctor() --- End of inner exception stack trace --- at Scriban.Functions.BuiltinFunctions..ctor() at Scriban.TemplateContext..ctor(ScriptObject builtin, IEqualityComparer`1 keyComparer)
解决方法
1. 添加程序集绑定重定向
在项目的App.config(桌面项目)或Web.config(Web项目)中,添加System.ValueTuple的绑定重定向配置,将旧版本请求重定向到项目中实际安装的版本:
<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" /> <!-- 根据实际安装版本调整newVersion的值,例如4.0.3.0或4.5.0 --> <bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" /> </dependentAssembly> </assemblyBinding> </runtime>
可在packages.config中查看System.ValueTuple的实际版本号,确保newVersion与之匹配。
2. 确保System.ValueTuple包已正确安装
如果项目中未引用System.ValueTuple,通过NuGet安装兼容版本:
Install-Package System.ValueTuple -Version 4.5.0
3. 清理重建项目
清理项目的bin和obj目录,重新编译运行,确保配置和引用生效。
内容的提问来源于stack exchange,提问作者mortenma71
相关产品推荐
相关产品推荐

