wpf 为什么在我的 .Net WCF Web 服务中创建 Oracle 连接时会收到“System.TypeInitializationException”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12060453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:04:05  来源:igfitidea点击:

Why am I getting a 'System.TypeInitializationException' when I create an Oracle connection in my .Net WCF web service?

.netwpfwcforacleodac

提问by JohnB

I'm currently working on implementing a WCF service which talks to an Oracle 10g database on the backend. When I attempt to connect to the database from within my service, I get the following exception: System.TypeInitializationException.

我目前正在实施 WCF 服务,该服务与后端的 Oracle 10g 数据库对话。当我尝试从我的服务中连接到数据库时,出现以下异常:System.TypeInitializationException.

  • I'm running on a windows 7 (64 bit machine).
  • I'm using VS 2010.
  • I'm using Oracle 10g on the backend.
  • I'm the Oracle.DataAccess.dll (ODP.Net) from the following installed instance

    c:\oracle\product.2.0\.....\ODP.NET\bin.x\Oracle.DataAccess.dll
    
  • I've included this DLL in the references for my WCF service.
  • I've included this service as a project within my VS WPF solution.
  • The service starts up automatically when I start my WPF application in VS 2010.
  • 我在 Windows 7(64 位机器)上运行。
  • 我正在使用 VS 2010。
  • 我在后端使用 Oracle 10g。
  • 我是来自以下已安装实例的 Oracle.DataAccess.dll (ODP.Net)

    c:\oracle\product.2.0\.....\ODP.NET\bin.x\Oracle.DataAccess.dll
    
  • 我已经在我的 WCF 服务的参考中包含了这个 DLL。
  • 我已将此服务作为项目包含在我的 VS WPF 解决方案中。
  • 当我在 VS 2010 中启动 WPF 应用程序时,该服务会自动启动。

At runtime I see the following output in my debug output window:

在运行时,我在调试输出窗口中看到以下输出:

'WcfSvcHost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC\Oracle.DataAccess\1.102.2.20__89b483f429c47342\Oracle.DataAccess.dll' A first chance exception of type 'System.TypeInitializationException' occurred in Oracle.DataAccess.dll

'WcfSvcHost.exe'(托管(v4.0.30319)):加载'C:\Windows\assembly\GAC\Oracle.DataAccess\1.102.2.20__89b483f429c47342\Oracle.DataAccess.dll'类型异常'System.TypeInitialization的第一次机会异常' 发生在 Oracle.DataAccess.dll 中

Sure enough, I look at the exception details in the debugger and I see the following information (this occurs when I attempt to create a new ORACLE connection):

果然,我查看了调试器中的异常详细信息,看到了以下信息(当我尝试创建新的 ORACLE 连接时会出现这种情况):

"The type initializer for 'Oracle.DataAccess.Client.OracleConnection' threw an exception." InnerException {"The provider is not compatible with the version of Oracle client"} System.Exception {Oracle.DataAccess.Client.OracleException}

“'Oracle.DataAccess.Client.OracleConnection' 的类型初始值设定项引发异常。” InnerException {“提供程序与 Oracle 客户端版本不兼容”} System.Exception {Oracle.DataAccess.Client.OracleException}

A Little Background:

一点背景:

I'm basically migrating my data layer to a WCF service. The same service layer previously existed in a WinForm application. Everything works fine in my WinForm application. I include the same reference to the Oracle.DataAccess.dll and all my reads/writes with the database work fine.

我基本上是将我的数据层迁移到 WCF 服务。相同的服务层以前存在于 WinForm 应用程序中。在我的 WinForm 应用程序中一切正常。我包含对 Oracle.DataAccess.dll 的相同引用,并且我对数据库的所有读/写都工作正常。

Is there something obviously different between the WCF and WinForm access/use of the DLL? Is there some limitation I'm unaware of? Is there an issue running this through VS2010 in the debugger?

WCF 和 WinForm 访问/使用 DLL 之间是否有明显不同?是否有一些我不知道的限制?在调试器中通过 VS2010 运行这个有问题吗?

I have absolutely no idea what is going on here. Any hints/direction would be greatly appreciated.

我完全不知道这里发生了什么。任何提示/方向将不胜感激。

回答by Daniel Hilgarth

No, there is nothing special on the .NET side you need to be aware of.
Some of the reasons for this specific error are:

不,在 .NET 方面没有什么特别需要注意的。
此特定错误的一些原因是:

  1. Your application targets .NET 4 but you only have ODP.NET for .NET 2 or even .NET 1 installed
  2. You are making use of a local Oracle Instant client and are missing some of the unmanaged DLLs.
  1. 您的应用程序面向 .NET 4,但您只安装了用于 .NET 2 甚至 .NET 1 的 ODP.NET
  2. 您正在使用本地 Oracle Instant 客户端并且缺少一些非托管 DLL。


Actually, your exception message states that it tried to load the Oracle.DataAccess.dll from the GAC with the version number 1.102.2.20. This seems to be the .NET 1version of ODP.NET as the version for .NET 2 has the number 2.102.2.20 and for .NET 4 it has the number 4.102.2.20.

实际上,您的异常消息指出它尝试从 GAC 加载版本号为 1.102.2.20 的 Oracle.DataAccess.dll。这似乎是ODP.NET的.NET 1版本,因为 .NET 2 的版本号为2.102.2.20,而 .NET 4 的版本号为4.102.2.20。

Maybe your Winforms application had a version of Oracle.DataAccess.dll in its bin folder and was using that instead the one in the GAC?

也许您的 Winforms 应用程序在其 bin 文件夹中有一个版本的 Oracle.DataAccess.dll 并且正在使用该版本而不是 GAC 中的版本?



These problems are the reason why I switched to a local Oracle Instant client in my projects. This adds 130 MB to my deployment but it doesn't matter as it's all corporate software.

这些问题是我在项目中切换到本地 Oracle Instant 客户端的原因。这为我的部署增加了 130 MB,但这并不重要,因为它都是公司软件。