问题点
在win7中使用CS-Script.Core脚本引擎进行脚本测试时,发现脚本引擎无法执行。同样的代码在win10中正常运行,无报错。报错如下:
System.IO.FileLoadException: 'Could not load file or assembly "System.ValueTuple, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" or one of its dependencies. The definition of the assembly manifest found does not match the reference to the assembly.
最初怀疑是win7和win10的dotnet环境不一致导致的,但安装环境后无果。最后在这里找到了解决办法。
解决办法
编辑项目文件.csproj,添加如下片段:
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
同时在app.config中添加
<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.2.0" newVersion="4.0.2.0" />
</dependentAssembly>
</assemblyBinding>
再重新编译,即可解决。
原因分析
个人分析,请谨慎参考。
官方文档
win7和win10中的System.ValueTuple的版本不一致。而代码在win10中编译打包,清单中是win10版本的dll,和win7的不匹配。
而设置AutoGenerateBindingRedirects true会自动添加到应用程序配置文件中以覆盖程序集统一