C#应用程序.NET程序集版本冲突问题求助
问题场景
开发的C#应用在Visual Studio中运行正常,但将项目引用提取并通过安装程序添加到GAC后,调用System.Text.Json时抛出以下异常:
System.IO.FileLoadException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
项目.csproj中引用的是System.Runtime.CompilerServices.Unsafe 6.0.0版本,且已成功安装到GAC。启用程序集绑定失败日志后发现,是System.Memory在引用4.0.4.1版本的Unsafe组件。
无效尝试
尝试添加了程序集绑定重定向配置的app.config(与可执行文件同目录、同名),但报错仍然存在:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <dependentAssembly> <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> </dependentAssembly> </configuration>
有效解决方案
采用Soleil的建议,直接将项目编译输出bin目录下的所有文件打包到安装程序中,而非仅提取.csproj中的引用安装到GAC,问题得以解决。
后续操作
目前需要更新安装程序,将bin目录的完整文件集作为部署内容,替代原有的仅GAC安装引用的方式。
内容的提问来源于stack exchange,提问作者Tom

