我们的测试机器上有个很奇怪的bug。错误是:

系统。来自程序集“activeviewer(…)”的类型“DummyItem”中的方法“SetShort”没有实现。

我就是不明白为什么。SetShort在DummyItem类中,我甚至重新编译了一个版本,写入事件日志,只是为了确保它不是部署/版本控制问题。奇怪的是,调用代码甚至不调用SetShort方法。


当前回答

当我的集成测试项目试图加载一个不包含接口依赖项解析的DLL时,我遇到了这个错误:

集成测试项目(参考主项目,但不是 StructureMap) 主要项目(引用StructureMap项目-使用 类构造函数中的接口) StructureMap项目(IoC - For().Use();)

这将导致抛出错误,因为它无法找到具体的实现。我在测试配置中排除了DLL,错误消失了

其他回答

I had the same problem. I figured out that my assembly, which is loaded by the main program, had some references with "Copy Local" set to true. These local copies of references were looking for other references in the same folder, which did not exist because the "Copy Local" of other references was set to false. After the deletion of the "accidentally" copied references the error was gone because the main program was set to look for correct locations of references. Apparently the local copies of the references screwed up the sequence of calling because these local copies were used instead of the original ones present in the main program.

重要的信息是,出现此错误是因为缺少加载所需程序集的链接。

只是补充一下我的经验,因为其他答案没有涵盖:

我在MsTest中运行单元测试时遇到了这个问题。

被测试的类位于有符号的程序集中。

该程序集的不同版本恰好在GAC中(但具有相同的程序集版本号)。

强命名程序集的依赖项解析算法与无符号程序集略有不同,因为首先检查的是GAC。

因此,MsTest正在获取GAC'd程序集,而不是使用bin文件夹中的程序集,并试图对其运行测试,这显然不起作用。

解决方案是拆除GAC总成。

注意,对我来说,提示是在运行测试时,在构建服务器上没有发生这种情况,因为构建将使用新的程序集版本号编译程序集。这意味着GAC中较旧版本的程序集将不会被拾取。

此外,我在这里找到了一个潜在的解决方案,如果您由于某种原因无法访问GAC: https://johanleino.wordpress.com/2014/02/20/workaround-for-unit-testing-code-that-reside-in-the-gac/

为我解决的问题是添加以下到ProjectReference和/或PackageReference以及<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>在正在加载的程序集中:

<Private>false</Private>
<ExcludeAssets>runtime</ExcludeAssets>

这使我的项目文件看起来像这样:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
  </PropertyGroup>

  <!-- For a package -->
  <ItemGroup>
    <PackageReference Include="SomePackage" Version="x.x.x.x">
      <Private>false</Private>
      <ExcludeAssets>runtime</ExcludeAssets>
    </PackageReference>
  </ItemGroup>

  <!-- For a project -->
  <ItemGroup>
    <ProjectReference Include="..\SomeProject\SomeProject.csproj">
      <Private>false</Private>
      <ExcludeAssets>runtime</ExcludeAssets>
    </ProjectReference>
  </ItemGroup>
</Project>

我在WCF服务中得到了这个,因为选择了x86构建类型,导致箱子在bin\x86下而不是bin下。选择Any CPU会导致重新编译的dll到达正确的位置(我不会详细说明这是如何发生的)。

Our problem solved with updating windows! Our web application is on .Net 4.7.1 and c# 7.0. As we tested in different windowses, we understood that the problem will be solved by updating windows. Indeed, the problem was seen in windows 10 (version 1703) and also in a windows server 2012(not updated in last year). After updating both of them, the problem was solved. In fact, the asp.net minor version(the third part of the clr.dll version in C:\Windows\Microsoft.NET\Framework64\v4.0.30319 ) was changed a bit after the update.