the type initializer for '' threw an exception

问题:程序启动时初始化主窗口类时,弹出该错误。
调查:查看类的构造函数是否会有异常抛出。
解决:去掉类的构造函数中可能出现的异常。
//////////////////////
环境:windows 2003 x64,Oracle 10g x64,odp.net(正确安装),.net framework 4
问题:.net应用程序通过Oracle.DataAccess.dll访问64位的Oracle服务器,在连接时出现以下异常:“System.TypeInitializationException:
The type initializer for 'Oracle.DataAccess.Client.OracleConnection'
threw an exception. ---> Oracle.DataAccess.Client.OracleException:
提供程序与此版本的 Oracle 客户机不兼容

通过windows的事件查看器,发现类似如下应用程序错误:
事件类型:错误
事件来源:
事件种类:无
事件 ID:0
日期:2011-10-20
事件:15:25:18
用户:N/A
计算机:
描述:
Service cannot be started. System.TypeInitializationException: The type
initializer for 'Oracle.DataAccess.Client.OracleConnection' threw an
exception. ---> Oracle.DataAccess.Client.OracleException:提供程序与此版本的 Oracle 客户机不兼容

事件类型:错误
事件来源:.NET Runtime 4.0 Error Reporting
事件种类:无
事件 ID:5000
日期:2011-10-20
事件:15:25:19
用户:N/A
计算机:
描述:
EventType clr20r3, P1 dbsupport.exe, P2 1.0.0.0, P3 4e9d2829, P4
oracle.dataaccess, P5 4.112.2.0, P6 4cea1964, P7 6cc, P8 1f5, P9
oracle.dataaccess.client.oracle, P10 NIL.

如下系统错误:
事件类型:错误
事件来源:SideBySide
事件种类:无
事件 ID:59
日期:2011-10-20
事件:15:25:18
用户:N/A
计算机:
描述:
Generate Activation Context 为 C:\odp.net\bin\OraOps11w.dll 失败。 参考错误消息: 参照的汇编没有安装在系统上

事件类型:错误
事件来源:SideBySide
事件种类:无
事件 ID:32
日期:2011-10-20
事件:15:39:28
用户:N/A
计算机:
描述:
找不到附属汇编 Microsoft.VC80.CRT,上一个错误是 参照的汇编没有安装在系统上。

事件类型:错误
事件来源:SideBySide
事件种类:无
事件 ID:59
日期:2011-10-20
事件:15:39:28
用户:N/A
计算机:
描述:
Resolve Partial Assembly 为 Microsoft.VC80.CRT 失败。 参考错误消息: 参照的汇编没有安装在系统上。

原因
服务器安装的.net framework 中缺少以下两个包:
Microsoft Visual C++ 2005 Redistributable Package,
Microsoft Visual C++ 2005 SP1 Redistributable Package

解决方法
下载上述包,并安装到服务器上。
下载地址:
http://www.microsoft.com/download/en/details.aspx?id=21254
http://www.microsoft.com/download/en/details.aspx?id=18471

/////////////////////

今天写程序想实现一个Singleton模式,结果程序老是抛异常说"The type initializer for 'TestStatic.StaticClass' threw an exception.",InnerException是"Object reference not set to an instance of an object",后来发现犯了一个非常低级的错误,代码如下:

// In main.cs

static void Main()
    {
      string t = StaticClass.StrMember;
      System.Diagnostics.Debug.WriteLine(t);

...
    }

// StaticClass.cs
    private StaticClass(int defValue)
    {
      Trace.WriteLine("StaticClass ctor");
      IntMember = defValue;
    }

private static StaticClass _instance = new StaticClass(12);
    private int intMember;
    public static int IntMember
    {
      set
      {
        Trace.WriteLine("set IntMember");
        _instance.intMember = value;
      }
    }

看出问题了吗?问题就出在

IntMember = defValue;

这句话,为这个静态属性赋值的时候其实是为静态成员_instance的intMember成员赋值,而此时_instance还是null,必须等待构造函数结束以后_instance才会被赋值为新建StaticClass对象的引用。所以应该把

IntMember = defValue;

改写为

this.intMember = defValue;

就一切正常了。调了很久,实在是很郁闷...

总结如下:首先当然是编程水平问题
:P,对Singleton模式一知半解。另外在构造函数里初始化静态属性也不是可取的做法。第三,之所以调试了很久,是因为Visual
Studio没有明确的定位异常发生的代码段:总是在main函数的"string t =
StaticClass.StrMember;"语句处报异常,非常的misleading,有时间需要研究一下为什么...

//////////////////////
主要是不能连接ORACLE数据库,连接DLL用的是 版本为2.112.1.2的ORACLE.DATAACCESS.DLL,在我自己的电脑上运行没有问题,在服务器端运行就会抛出异常。

为了解决问题我尝试了用更新的版本。但是他还是会抛出同样的问题。

最后我想起了.NET中自带有微软的ORACLE访问借口,于是我就用了SYSTEM.DATA.ORACLECLIENT这个组件,问题得到了解决。

部分代码:(注释的就是调用的ORACLE.DATAACCESS.DLL组件的方法)

  protected DataSet GetData()
{ //Oracle.DataAccess.Client.OracleConnection conn = new Oracle.DataAccess.Client.OracleConnection(connstr);
System.Data.OracleClient.OracleConnection conn = new OracleConnection(connstr);
try
{
conn.Open();
//Oracle.DataAccess.Client.OracleCommand com = conn.CreateCommand();
OracleCommand com = conn.CreateCommand();
com.CommandText = "select * from DL_GOVT_NOTICE_INFO";//"select * from DL_GOVT_NOTICE_EXP_HIST";
//Oracle.DataAccess.Client.OracleDataAdapter apter = new Oracle.DataAccess.Client.OracleDataAdapter(com);
OracleDataAdapter apter = new OracleDataAdapter(com);
DataSet ds = new DataSet();
apter.Fill(ds);
apter.Dispose();
conn.Dispose();
return ds;
}
catch (Exception ex)
{
this.LabShowInfo.Text = "Error: " + ex.Message;
conn.Close();
return null;
} }

详细情况我现在也还很糊涂,希望对有出现类似情况的同志有所帮助。

//////////////////

Both Oracle Data Provider for .NET (from Oracle) and .NET Framework Data Provider for Oracle (from Microsoft) require Oracle Client installed on machine.

/////////

the type initializer for '' threw an exception的更多相关文章

  1. C#中异常:“The type initializer to throw an exception(类型初始值设定项引发异常)”的简单分析与解决方法

    对于C#中异常:“The type initializer to throw an exception(类型初始值设定项引发异常)”的简单分析,目前本人分析两种情况,如下: 情况一: 借鉴麒麟.NET ...

  2. .net core获取数据库连接 抛出The type initializer to throw an exception

    原文:https://www.cnblogs.com/pudefu/p/7580722.html 在.NET Framework框架时代我们的应用配置内容一般都是写在Web.config或者App.c ...

  3. 使用Spire组件抛出异常The type initializer for 'spr857' threw an exception

    使用Spire组件抛出异常The type initializer for 'spr857' threw an exception 我使用免费的Spire.Xls组件尝试去转换Excel文档到PDF文 ...

  4. System.Security.SecurityException The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception

    [15/08/19 00:03:10] [DataManager-7292-ERROR] System.Reflection.TargetInvocationException: Exception ...

  5. System.TypeInitializationException: The type initializer for 'Mono.Unix.Native.Stdlib' threw an exception.

    08-31 17:02:03: ### DEBUG ##########################System.TypeInitializationException: The type ini ...

  6. debian The type initializer for 'System.Drawing.KnownColors' threw an exception

     Change the "System.Drawing" reference of "CoreCompat.System.Drawing"if you thro ...

  7. ASP.Net Core "The type initializer for 'Gdip' threw an exception"

    ASP.NET Core项目部署在Linux下可能会出现GDI错误 The type initializer for 'Gdip' threw an exception 解决方案:创建 libdl 的 ...

  8. The type initializer for System.Data.SqlClient.SqlConnection threw an exception

    The type initializer for System.Data.SqlClient.SqlConnection threw an exception net framwork啥原因 xp电脑

  9. System.TypeInitializationException: The type initializer for 'Oracle.DataAccess.Client.OracleConnection' threw an exception. ---> Oracle.DataAccess.Client.OracleException: 提供程序与此版本的 Oracle 客户机不兼容”

    .net应用程序通过Oracle.DataAccess.dll访问64位的Oracle服务器,在连接时出现以下异常:“System.TypeInitializationException: The t ...

随机推荐

  1. javascript高级编程笔记06(面相对象2)

    1)  构造函数模式 es中的构造函数可以用来创建特定类型的对象,像Object和Array这样的原生构造函数,在运行时会自动出现在执行环境中,此外,也可以创建自定义的构造函数,从而定义自定义对象类型 ...

  2. C#结合js 上传文件和删除文件(技术点有:asp.net mvc ,nhibernate,ajax等)

    之前做项目的时候要用到上传文件的功能,现在我总结一下,上传文件和删除文件的代码,在以后的使用的过程中也更方便查找. [HttpPost] public ActionResult EditUser() ...

  3. poj 3463 Sightseeing( 最短路与次短路)

    http://poj.org/problem?id=3463 Sightseeing Time Limit: 2000MS   Memory Limit: 65536K Total Submissio ...

  4. c++ 16 this 和 继承 及继承机制中的构造函数 与 析构函数

    #include <iostream> #include <string> using namespace std; class Animal { public: Animal ...

  5. ural 1123

    找大于等于原数的最小回文数字  代码比较烂........... #include <iostream> #include <cstdio> #include <cstr ...

  6. 从iPhone4、iPhone5、iPhone6看手机外壳加工工艺进化史

    从iPhone4.iPhone5到iPhone6,苹果为我们推出了一代又一代新产品,让我们享受到最新的科技产品.每次不只是配置上的改变,苹果在工艺上也不断改变.下面就阐述一下我对这几款手机在设计和制造 ...

  7. classpath、path、JAVA_HOME的作用及JAVA环境变量配置

    CLASSPATH是什么?它的作用是什么? 它是javac编译器的一个环境变量.它的作用与import.package关键字有关.当你写下improt java.util.*时,编译器面对import ...

  8. 第二个C语言代码

    有问题,还没找出哪里出错了       输入一串字符,问号结束 统计1~9各出现的次数 ******************************************************** ...

  9. Tableau

    http://tableau.analyticservice.net/desktop.html

  10. GCC警告选项例解

    程序员是追求完美的一族,即使是一般的程序员大多也都不想看到自己的程序中有甚至那么一点点的瑕疵.遇到任意一条编译器警告都坚决不放过.有人会说:我们可以使用比编译器更加严格的静态代码检查工具,如splin ...