nHibernate 未在命令行的 MSTests 运行 中创建 Oracle 驱动程序

nHibernate not creating Oracle driver in MSTests run on command line

我已经在这个特定问题上工作了几个星期了,我非常沮丧。因此,我会尽我所能提供所有信息,并希望最好。

我的团队正在构建一个新的应用程序。这是字母汤:

我正在将应用程序编译为 32 位应用程序,并且我已确认我安装了 32 位版本的 Oracle。

我们已经通过 MSTest 运行 为 NHibernate 映射编写了一些测试。当我们运行这些通过Visual Studio的测试浏览器时,它们都运行正确并通过。应用程序本身也可以正确编译和部署。我们通过检查步骤之间的数据库验证了测试 运行 正确,因此我们相当确定测试本身不是问题。

当我们通过命令行 运行 MSTest 时,我们收到以下错误:

Initialization method MyTests.Setup threw exception. NHibernate.HibernateException: NHibernate.HibernateException: Could not create the driver from NHibernate.Driver.OracleDataClientDriver. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: Unable to find the requested .Net Framework Data Provider. It may not be installed..

我试过重新安装 Oracle 没有效果。我已经尝试检查 machine.config 文件是否有错误(如其他帖子中所建议的那样)并找到 none.

我们的Fluent配置如下:

OracleDataClientConfiguration.Oracle10
    .ConnectionString(connectionString)
    .Driver("NHibernate.Driver.OracleDataClientDriver")
    .ShowSql()
    .FormatSql();

我在命令行中 运行 的代码如下:

(cd to the directory where the test .dll is)
>"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\MSTest.exe" /testcontainer:MyTests.dll /test:UnitTests

我觉得我在这里遗漏了什么。有什么想法吗?


更新:找到解决方案

这是一个奇怪的问题。我按照下面 F运行 的解决方案安装了 Oracle.ManagedDataAccess 包,并将上面配置中的 NHibernate 驱动程序更改为 NHibernate.Driver.OracleManagedDataClientDriver。根据我们的快速评论讨论,这导致了一个新错误:

Initialization method MyTests.Setup threw exception. NHibernate.HibernateException: NHibernate.HibernateException: Could not create the driver from NHibernate.Driver.OracleManagedDataClientDriver. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Configuration.ConfigurationErrorsException: Failed to find or load the registered .Net Framework Data Provider

F运行 然后引导我到 another SO question,这鼓励我逐个检查 Oracle 配置。有什么比创建测试更好的方法呢?

var x = new OracleConnection(connectionString);
x.Open();
Assert.IsTrue(x.State == System.Data.ConnectionState.Open);
x.Close();
Assert.IsFalse(x.State == System.Data.ConnectionState.Open);

在我快速尝试 运行 这个测试的过程中,我 运行 使用我上面提到的脚本的整个 UnitTests 集合。瞧,每个测试都通过了! 做我的尽职调查,尝试以下

出于某种原因,新驱动程序的组合和在测试中明确引用它似乎已经解决了这个问题。我对任何关于原因的理论持开放态度,但我敢打赌这可以作为一个新问题。

我将停止使用位特定版本的 oracle 驱动程序并转而使用托管驱动程序 (https://www.nuget.org/packages/Oracle.ManagedDataAccess/)。它有点不可知,并且根本不需要您安装 Oracle 客户端。

我实际上找到了问题的解决方案,这一切都与 Oracle.DataAccess.dll 文件在运行时的加载方式有关(披露:我在同一个项目中使用 wadeb)。

似乎在服务器上的每个位置都在搜索 Oracle.DataAccess.dll 除了 Jenkins 工作区中的构建输出文件夹,因此正在拉取 DLL来自 GAC 的文件。

用于查找 DLL 文件的文件路径之一是 "current executable" 所在的文件夹。在我们的例子中,"current executable" 是 mstest.exe。将 Oracle.DataAccess.dll 文件复制到 C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE 就成功了。

成功了吗?是的

这是黑客攻击吗?绝对 - 但现在它无需升级到托管 Oracle 驱动程序即可工作。

我们的服务器没有使用与托管驱动程序一起工作的 Oracle 客户端,在服务器升级之前,持续集成构建是不可接受的。

我遇到了同样的错误,我将我的测试切换到 x64,它现在运行得很好: