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. TOPAPI 消息通知机制

    接收用户订阅消息 public class UserSubMain { public static void main(String[] args ) throws ApiException { St ...

  2. bnuoj 20834 Excessive Space Remover(水水)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=20834 [题意]: 每次减少一半的空格,问经过多少次操作能得到每个单词之间的空格为1,输入字符串大小 ...

  3. 然爸读书笔记(2013-5)----Rework(重来)

    (1)你没有必要耗尽你一生的积蓄,承担财务风险. (2)你可以一边继续日常工作,一边开始创业,这样随时都能有现金满足需要.你甚至不需要办公室. 现在可以在家工作,和从未见面离你千里之外的人合作. (3 ...

  4. iOS sqlite 增删改查 简单封装(基于 FMDB)

    /** *  对 sqlite 的使用进行简单封装,仅涉及简单的单表 增删改查 * *  基于 FMDB * *  操作基于 model ,数据库表字段与 model 属性一一对应,对 model 整 ...

  5. 1483:[HNOI]2009 梦幻布丁 - BZOJ

    Description N个布丁摆成一行,进行M次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色.例如颜色分别为1,2,2,1的四个布丁一共有3段颜色. Input 第 ...

  6. 4.0 spring-注册解析的Bean

    1.0 registerBeanDefinition 对于配置文件,解析也解析完了,装饰也装饰完了,对于得到的BeanDefinition已经可以满足后续的使用了,唯一剩下的工作就是注册了, 也就是: ...

  7. [转载]MVC3缓存:使用页面缓存

    在以前的WebForm的开发中,在页面的头部加上OutputCache即可启用页面缓存,而在MVC3中,使用了Razor模板引擎的话,该如何使用页面缓存呢?如何启用 在MVC3中要如果要启用页面缓存, ...

  8. Java 内存结构备忘录

    本文详细描述了 Java 堆内存模型,垃圾回收算法以及处理内存泄露的最佳方案,并辅之以图表,希望能对理解 Java 内存结构有所帮助.原文作者 Sumith Puri,本文系 OneAPM 工程师编译 ...

  9. HDU1437+模拟

    枚举中间可能出现的天气 #include<stdio.h> #include<string.h> #include<stdlib.h> ; ][ ]; void s ...

  10. ubuntu无线上网静态ip配置以及配置静态IP 之后无法正常上网的解决方案

    一. 配置无线网络的静态IP 编辑/etc/network/interfaces文件如下: auto lo wlan0 iface lo inet loopback iface wlan0 inet ...