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. 使用Entity Framework时要注意的一些性能问题

    http://diaosbook.com/Post/2012/12/9/performance-issue-in-select-one-or-few-colums-via-entityframewor ...

  2. 【BZOJ 2654】tree

    Description 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树. 题目保证有解. Input 第一行V,E,need分别表示点数,边数和需要的白色 ...

  3. SDIBT 3237 Boring Counting( 划分树+二分枚举 )

    http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=3237 Problem H:Boring Counting Time Limit: 3 Sec  ...

  4. PHPCMS搭建wap手机网站

    PHPCMS搭建PC端网站比较方便,但是在wap手机端方面却不怎么实用,而且自带的手机建站感觉不是很好,而且模版不好控制,现在对其进行修改,手机建站个人感觉比较方便 首先在phpcms/libs/fu ...

  5. hibernate多对一单向外键

    hibernate多对一单向外键: 描述:

  6. 如何设置Samza的metrics

    参考这个里边对API的调用 http://samza.incubator.apache.org/learn/documentation/0.7.0/container/metrics.html 参考这 ...

  7. Making your local server accessible from anywhere

    In reality you probably don’t want to host you websites on your local computer unless you have a ver ...

  8. hdu 3864 D_num

    思路:给一个数n,是否只有4个约数(包括1),也就是找3个大于1的约数. 而任何一个数都可由质数表示,所以对于给定的数,只需要进行质因数分解.这里有 2种情况:如果有3个一样的质因数,则满足条件:否则 ...

  9. 深入剖析Classloader(二)--根类加载器,扩展类加载器与系统类加载器

    原文地址:http://yhjhappy234.blog.163.com/blog/static/31632832201152555245584/?suggestedreading&wumii ...

  10. 镜面电火花EDM加工技术资料,模具行业的人应该好好看看!

    目前镜面电火花加工技术在精密型腔模具制造中逐步得以推广.本文就企业中镜面电火花加工应用的关键环节,结合实践分析了影响镜面加工性能的因素.通过控制各个工艺环节,可有效实现高质量.高效率的镜面电火花加工. ...