系统.IO.FileNotFoundException: 运行单元测试时出现文件或程序集“Oracle.DataAccess”的加载错误...

5
我想运行一些单元测试,直接在 Oracle 数据库中执行一些 SQL 命令,但当单元测试开始时,我遇到了以下异常:
Testmethod1 抛出异常: System.IO.FileNotFoundException: 找不到文件或程序集 'Oracle.DataAccess, Version=4.112.3.0, Culture=neutral, PublicKeyToken=89b483f429c47342' 或它的某个依赖项。系统无法找到指定的文件。
我已经在项目引用中添加了 Oracle.DataAccess (通过 NuGet 添加),并安装了 Oracle 数据提供程序。
甚至我还有其他一些使用 Oracle.DataAccess 的项目,可以在这台机器上运行而没有问题。似乎只有 Microsoft.Net 单元测试框架 (MSTest) 不能加载该程序集。
下面是 Fusion Log Viewer 中记录的日志:
*** Assembly Binder Log Entry  (23/08/2013 @ 04:50:07 p.m.) ***

The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\QTAgent32_40.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: User = ...
LOG: DisplayName = Oracle.DataAccess, Version=4.112.3.0, Culture=neutral, PublicKeyToken=89b483f429c47342
 (Fully-specified)
LOG: Appbase = file:///...C:/<solution_folder>.../TestResults/.../Out
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = QTAgent32_40.exe
Calling assembly : ...<project_name>..., Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\...<solution_folder>...\TestResults\...\Out\...<project_name>....DLL.config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Oracle.DataAccess, Version=4.112.3.0, Culture=neutral, PublicKeyToken=89b483f429c47342
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/...<solution_folder>.../TestResults/.../Out/Oracle.DataAccess.DLL.
LOG: Attempting download of new URL file:///C:/...<solution_folder>.../TestResults/.../Out/Oracle.DataAccess/Oracle.DataAccess.DLL.
LOG: Attempting download of new URL file:///C:/...<solution_folder>.../TestResults/.../Out/Oracle.DataAccess.EXE.
LOG: Attempting download of new URL file:///C:/...<solution_folder>.../TestResults/.../Out/Oracle.DataAccess/Oracle.DataAccess.EXE.
LOG: All probing URLs attempted and failed.

它表明在GAC中没有找到程序集,但我已经找到了它:C:\Windows\Microsoft.NET\assembly\GAC_64\Oracle.DataAccess\v4.0_4.112.3.0__89b483f429c47342\Oracle.DataAccess.dll\

值得注意的是上面的日志显示QTAgent32_40.exe,即使我在Visual Studio菜单中选择了TEST -> Test Settings -> Default Processor Architecture -> X64。在这之前,我收到了一个BadImageFormatException

附加说明:

  • 我安装的Oracle数据提供程序是64位版本。
  • 引用已经设置为Copy Local=True

1
打开融合日志以查看是否有助于找出缺失的文件:http://www.hanselman.com/blog/CommentView.aspx?guid=3654c8f3-c5c3-4dee-a01f-c9a8da3ef2fa - John Koerner
不知道这个。谢谢。我刚把它添加到问题中。 - mmutilva
你的测试是否以32位进程运行? - John Koerner
不,64位和从项目安装和引用的Oracle数据提供程序是64位版本...否则我会得到一个BadImageFormatException异常(之前遇到过)。 - mmutilva
一个快速的解决方法是将引用的“CopyLocal”设置为true,但它应该可以从GAC中工作。 - John Koerner
该引用已经具有 CopyLocal = True - mmutilva
1个回答

6
发现了问题。
无论我选择 TEST -> Test Settings -> Default Processor Architecture -> X64,仍有一个 .testsettings 文件强制测试以 32 位模式运行。
奇怪的是,如果我选择 X86,运行时会出现适当的 BadImageFormatException,而如果架构在 .testsettings 文件中被强制执行,则会出现一些误导性的FileNotFoundException

我曾经遇到过同样的问题,而你的回答指引了我正确的方向。在VS 2017 Professional中,我没有找到.testsettings文件,但我在另一个类似的StackOverflow问题中找到了这个答案。转到测试->测试设置\\取消选中....projectTestSettings.testsettings文件\\再次运行测试。我没有在这个测试设置文件中看到任何指示X86执行的内容,但它成功地解决了FileNotFoundException错误。 - Hillarie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接