我们与MongoDB一起使用的每个项目都会在没有加载的System.Runtime.InteropServices库中出现问题.
这次错误很有趣:
外部异常找不到lib的4.3.0.0版本.但是内部异常找不到版本4.0.0.0
有没有人对此有所了解?
有关此问题的更多信息:
所以,NuGet已经安装了4.3.0.0
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="System.Runtime" version="4.3.0" targetFramework="net462" />
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net462" />
</packages>
Run Code Online (Sandbox Code Playgroud)
packages.config确认我已经安装了4.3.0.0,
然而,app.config似乎总是与现实不同步:
<dependentAssembly>
<assemblyIdentity name="System.Runtime.InteropServices.RuntimeInformation" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
</dependentAssembly>
Run Code Online (Sandbox Code Playgroud)
添加了关于版本4.0.1.0的一行
在同一行.. csproj是胡说八道:
<Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>x:\Packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
</Reference>
Run Code Online (Sandbox Code Playgroud)
因为它声称引用4.0.1.0的路径为4.3.0.0
有一些东西被破坏了,它总是发生在那个完全相同的lib上; 不仅仅是这个项目:在我包含MongoDB的任何地方,这个lib都是一个依赖项,每次都有一些随机问题.
当我尝试手动加载时:
var Name = new AssemblyName("System.Runtime.InteropServices.RuntimeInformation, Version=4.3.0.0");
var Asm = Assembly.Load(Name);
Run Code Online (Sandbox Code Playgroud)
它也失败了.
我发现System.Runtime.InteropServices.RuntimeInformation.dll不会被复制到build文件夹,即使它包含在项目中.
我找到了一个讨厌的解决方法:如果我在主exe中包含MongoDB,即使我不使用它,它依赖于Interop lib,这迫使lib被复制到build文件夹,然后后续调用工作.
我将尝试简化单元测试的问题。
我正在用静态私有字段测试方法。如果我运行测试,那么我必须能够预测静态字段的价值。当我有一个测试,或者一个一个地运行测试时,这不是问题。比一切都正常。
问题是当我有多个单元测试来更改静态值并全部运行时。在这种情况下,只有第一个测试通过,其他所有测试都将失败。
这是简化的示例:
using Xunit;
namespace My.Unit
{
public class UnitTests
{
[Fact]
public void test1()
{
var obj = new TestClass();
var res = obj.testMethod();
Assert.Equal(1, res);
}
[Fact]
public void test2()
{
var obj = new TestClass();
var res = obj.testMethod();
Assert.Equal(1, res);
}
}
public class TestClass
{
private static int staticValue = 0;
public TestClass()
{
++staticValue;
}
public int testMethod()
{
return staticValue;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我希望每个单元测试都具有静态字段的新寿命,但是事实并非如此。