Moq依赖升级后调用AsSpan()抛FileLoadException,该如何排查?
问题:System.Runtime.CompilerServices.Unsafe版本不匹配引发FileLoadException
依赖层级
Moq 4.18.4的依赖链如下:
Moq 4.18.4 System.Threading.Tasks.Extensions >= 4.5.4 System.Runtime.CompilerServices.Unsafe >= 4.5.3
强制使用的版本
我的应用必须使用:
System.Runtime.CompilerServices.Unsafe 4.7.0
从依赖定义来看,4.7.0满足>=4.5.3的要求,但执行调用System.Memory中AsSpan()的单元测试时,Debug和Release模式都会抛出以下异常:
异常信息
System.IO.FileLoadException HResult=0x80131040 Message=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) Source=System.Memory StackTrace: at System.MemoryExtensions.AsSpan(String text) at UnitTestProject1.UnitTest1.TestMethod1() in C:\source\repos\ConsoleApp9\UnitTestProject1\UnitTest1.cs:line 13
重现步骤
- 创建包含以下NuGet包的单元测试项目:
- Moq Version="4.20.69"
- MSTest.TestAdapter Version="2.2.8"
- MSTest.TestFramework Version="2.2.8"
- System.Memory Version="4.5.4"
- System.Runtime.CompilerServices.Unsafe Version="4.7.0"
- 运行测试,会在
b = a.AsSpan()行抛出上述异常。
测试代码
namespace UnitTestProject1 { using System; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { string a = "asdfdsa"; var b = a.AsSpan(); } } }
请问这是Bug还是我遗漏了必要配置?
内容的提问来源于stack exchange,提问作者NirMH
相关产品推荐
相关产品推荐

