我们的测试机器上有个很奇怪的bug。错误是:
系统。来自程序集“activeviewer(…)”的类型“DummyItem”中的方法“SetShort”没有实现。
我就是不明白为什么。SetShort在DummyItem类中,我甚至重新编译了一个版本,写入事件日志,只是为了确保它不是部署/版本控制问题。奇怪的是,调用代码甚至不调用SetShort方法。
我们的测试机器上有个很奇怪的bug。错误是:
系统。来自程序集“activeviewer(…)”的类型“DummyItem”中的方法“SetShort”没有实现。
我就是不明白为什么。SetShort在DummyItem类中,我甚至重新编译了一个版本,写入事件日志,只是为了确保它不是部署/版本控制问题。奇怪的是,调用代码甚至不调用SetShort方法。
当前回答
为我解决的问题是添加以下到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>
其他回答
我得到了一个“钻石”形的项目依赖:
项目A使用项目B和项目D 项目B使用项目D
我重新编译了项目A而不是项目B,这允许项目B“注入”旧版本的项目D dll
我一直在想这个问题…… 这里的许多答案都很好地解释了问题是什么,但没有解释如何解决问题。
解决方案是手动删除项目发布目录中的bin文件。它将清理所有引用,并强制项目使用最新的dll。
我不建议使用发布工具的Delete功能,因为这可能会使IIS失效。
另一种情况是,有符号程序集的版本不正确。这不是这种疾病的正常症状,但这是我患病的情况
asp.net项目包含程序集A和程序集B, B是强命名的 程序集A使用Activator。CreateInstance加载程序集C(即没有对单独构建的C的引用) C是引用程序集B的较旧版本构建的
希望这能帮助到一些人,我花了很长时间才弄明白。
我刚刚将一个解决方案从MVC3升级到MVC5,并开始从我的单元测试项目中收到相同的异常。
检查所有的引用寻找旧文件,最终发现我需要做一些bindingRedirects Mvc,在我的单元测试项目。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
还有另一种方法:
class GenericFoo<T> {}
class ConcreteFoo : GenericFoo<ClassFromAnotherAssembly> {}
程序集中的代码,不引用ClassFromAnotherAssembly的程序集。
var foo = new ConcreteFoo(); //kaboom
当ClassFromAnotherAssembly是ValueTuple时,这发生在我身上。