我正在尝试在C#Windows窗体应用程序(Visual Studio 2005)中运行一些单元测试,但出现以下错误:
System.IO.FileLoadException:未能加载文件或程序集“Utility,Version=1.2.0.200,Culture=neutral,PublicKeyToken=764d581291d764f7”或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。(HRESULT的异常:0x80131040)**位于x.Foo.FooGO()位于Foo.cs:line 123中的x.Foo.Foo2(String groupName_)位于FooTests.cs:line 98中的x.Foo.UnitTests.FooTests.TestFoo()**System.IO.FileLoadException:未能加载文件或程序集“Utility,Version=1.2.0.203,Culture=neutral,PublicKeyToken=764d581291d764f7”或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。(HRESULT的异常:0x80131040)
我查阅了我的参考资料,我只参考了实用程序版本1.2.0.203(另一个是旧版本)。
关于我如何找出试图引用此DLL文件的旧版本的内容,有什么建议吗?
此外,我想我的硬盘上甚至没有这个旧组件。是否有任何工具可以搜索此旧版本的程序集?
这是我解决这个问题的方法。
从异常消息中,获取“问题”库的名称和“预期”版本号。
在解决方案中查找该.dll的所有副本,右键单击它们,然后检查它是哪个版本的.dll。
好的,在这个例子中,我的.dll肯定是2.0.5022.0(所以异常版本号是错误的)。
在解决方案中的所有.csproj文件中搜索异常消息中显示的版本号。用dll中的实际版本号替换此版本号。
所以,在这个例子中,我将替换这个。。。
<Reference Include="DocumentFormat.OpenXml, Version=2.5.5631.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
…用这个。。。
<Reference Include="DocumentFormat.OpenXml, Version=2.0.5022.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
工作完成!
我添加了一个NuGet包,结果发现我的应用程序的黑盒部分引用了旧版本的库。
我删除了包并引用了旧版本的静态DLL文件,但web.config文件从未从以下位置更新:
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
恢复到卸载包时应该恢复的状态:
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.5.0.0" />
</dependentAssembly>
以下命令将任何程序集版本重定向到3.1.0.0版本。我们有一个脚本,它将始终更新App.config中的此引用,因此我们再也不用处理此问题了。
通过反射,您可以获取程序集publicKeyToken,并从.dll文件本身生成此块。
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Castle.Core" publicKeyToken="407dd0808d44fbdc" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-65535.65535.65535.65535" newVersion="3.1.0.0" />
</dependentAssembly>
</assemblyBinding>
注意,如果没有XML命名空间属性(xmlns),这将无法工作。