Error thrown by a dependency of object 'System.Data.SQLite' 解决办法

项目中用的.net4+mvc3+spring.net1.3.2+nhibernate32+SQLite 1.0.79版本,就目前来说,应该是最新的,

在使用的时候单独用nh链接sqlite是没问题的,但是用spring.net配置nhibernate问题就来了,

 

1.3.2的版本就支持SQLite1.0.72的版本,因为我用的是1.0.79的版本,如果写成System.Date.SQLite,报错为

注意红线的报错,我查看了Spring.net的源码,里面是这样写的,

 

为什么他会报这儿的错呢,因为后面还有句:

<alias name="SQLite-1.0.65" alias="System.Data.SQLite"/>

他把这个直接映射到了1.0.65的版本,而且里面最高的版本为1.0.72,没有我想要的1.0.79,解决办法很简单,自己再加一个就是了,注意看我第一张图:

<!--数据库提供者-->
  <db:additionalProviders resource="assembly://Mark/Mark.ConfigFiles/SQLite.xml"/>

这个SQLite.xml就是我自己写的,其实就是copy他的写法,我只是不知道那个PublicKeyToken是多少,直接删除了,一样的可以用,SQLite.xml里面的内容是:

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net" xmlns:db="http://www.springframework.net/database">
  <object id="SQLite-1.0.79" type="Spring.Data.Common.DbProvider, Spring.Data" singleton="false">
    <constructor-arg name="dbMetaData">
      <object type="Spring.Data.Common.DbMetadata">
        <constructor-arg name="productName" value="SQLite"/>
        <constructor-arg name="assemblyName" value="System.Data.SQLite, Version=1.0.79.0, Culture=neutral"/>
        <constructor-arg name="connectionType" value="System.Data.SQLite.SQLiteConnection, System.Data.SQLite, Version=1.0.79.0, Culture=neutral"/>
        <constructor-arg name="commandType" value="System.Data.SQLite.SQLiteCommand, System.Data.SQLite, Version=1.0.79.0, Culture=neutral"/>
        <constructor-arg name="parameterType" value="System.Data.SQLite.SQLiteParameter, System.Data.SQLite, Version=1.0.79.0, Culture=neutral"/>
        <constructor-arg name="dataAdapterType" value="System.Data.SQLite.SQLiteDataAdapter , System.Data.SQLite, Version=1.0.79.0, Culture=neutral"/>
        <constructor-arg name="commandBuilderType" value="System.Data.SQLite.SQLiteCommandBuilder, System.Data.SQLite, Version=1.0.79.0, Culture=neutral"/>
        <constructor-arg name="commandBuilderDeriveParametersMethod" value="not supported"/>
        <constructor-arg name="parameterDbType" value="System.Data.SQLite.TypeAffinity, System.Data.SQLite, Version=1.0.79.0, Culture=neutral"/>
        <constructor-arg name="parameterDbTypeProperty" value="DbType"/>
        <constructor-arg name="parameterIsNullableProperty" value="IsNullable"/>
        <constructor-arg name="parameterNamePrefix" value=":"/>
        <constructor-arg name="exceptionType" value="System.Data.SQLite.SQLiteException, System.Data.SQLite, Version=1.0.79.0, Culture=neutral"/>
        <constructor-arg name="useParameterNamePrefixInParameterCollection" value="true"/>
        <constructor-arg name="useParameterPrefixInSql" value="true"/>
        <constructor-arg name="bindByName" value="true"/>
        <!-- this is only true for .net 1.1 kept it here just in case we want to revert back to this strategy for
             obtaining error codes-->
        <constructor-arg name="errorCodeExceptionExpression" value="ErrorCode.ToString('D')"/>

        <property name="ErrorCodes.badSqlGrammarCodes">
          <value></value>
        </property>
        <property name="ErrorCodes.DataAccessResourceFailureCodes">
          <value>1</value>
        </property>
        <property name="ErrorCodes.DataIntegrityViolationCodes">
          <value></value>
        </property>
        <property name="ErrorCodes.CannotAcquireLockCodes">
          <value>15</value>
        </property>
        <property name="ErrorCodes.DeadlockLoserCodes">
          <value>5,6</value>
        </property>
      </object>
    </constructor-arg>
  </object>
</objects>


然后写:

<db:provider id="DbProvider" provider="SQLite-1.0.79" connectionString="Data Source=|DataDirectory|xxxx.db;Version=3;FailIfMissing=False;"/>

再运行,不报错了

### 关于 `TargetInvocationException` 的原因及解决方法 #### 原因分析 当通过反射调用方法时,如果目标方法抛出了未捕获的异常,则该异常会被封装成 `System.Reflection.TargetInvocationException` 并向外传播[^1]。这是因为反射机制本身并不知道如何处理目标方法中的具体错误逻辑,因此它将原始异常作为 `InnerException` 属性的一部分传递给调用方。 以下是可能导致此异常的一些常见场景: 1. **参数不匹配**:传入的目标方法参数数量或类型不符合其签名要求。 2. **访问权限不足**:尝试调用私有、受保护或其他不可见的方法而没有提供适当的安全上下文。 3. **运行时错误**:目标方法内部存在逻辑缺陷,在执行过程中触发了其他类型的异常(如 `NullReferenceException`, `ArgumentException` 等)。 4. **外部依赖失败**:例如数据库连接中断或者文件读取失败等问题可能间接引发此类异常。 #### 解决方案 为了妥善应对由反射引起的 `TargetInvocationException` ,可以采取如下措施: ##### 方法一:捕捉并解析内部异常 在捕获到 `TargetInvocationException` 后,应进一步检查其 `InnerException` 字段来获取实际发生的错误详情,并据此实施相应的补救策略[^2]: ```csharp try { var result = method.Invoke(instance, arguments); } catch (TargetInvocationException tie) { if (tie.InnerException != null && tie.InnerException is SpecificCustomException sce){ HandleSpecificError(sce.Message); } } ``` ##### 方法二:利用过滤器条件简化特定情况下的处理流程 对于已知可能出现某些预定义种类的异常情形下,可采用带筛选功能的异常处理器实现更精确控制流管理[^2]: ```csharp try{ method.Invoke(obj,params); }catch(Exception e)when(e.InnerException?.GetType()==typeof(KnownIssue)){ ResolveKnownIssues(); } ``` ##### 方法三:防止不必要的嵌套包装行为发生 有时开发者希望直接暴露底层真实问题而非层层包裹后的形式化表述;此时可通过设置特殊标志位告知框架无需再额外增加一层 TargetInvocationWrapper[^3]: ```xml <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <!-- Other bindings --> <dependentAssembly> <bindingRedirect oldVersion="0.0.0.0-99.99.99.99" newVersion="X.Y.Z"/> <publisherPolicy apply="no"/> </dependentAssembly> </assemblyBinding> </runtime> </configuration> ``` 注意以上XML配置片段仅作为一个示意例子展示如何调整应用程序加载规则从而影响最终表现效果,请根据实际情况修改适应自己的环境需求。 另外值得注意的是,尽管上述技术手段能够有效缓解部分棘手状况带来的困扰,但在日常开发实践中还是应当尽量减少过度依赖动态特性所带来的不确定性风险因素的影响程度为佳。 --- ### 示例代码演示 以下是一个完整的示例,展示了如何优雅地处理反射调用期间产生的各种潜在问题: ```csharp using System; using System.Linq.Expressions; public class Program { public static void Main(){ TryInvokeMethodSafely(() => DangerousOperation()); } private static void DangerousOperation(){ throw new InvalidOperationException("Something went wrong!"); } /// <summary> /// Safely invokes given action delegate and handles any exceptions gracefully. /// </summary> public static TOut SafeCall<TIn,TOut>(Func<TIn,TOut> func,TIn input)=> CallWithProtection(()=>func(input)); public static TResult CallWithProtection<TResult>(Expression<Func<TResult>> expr){ try{ return expr.Compile().Invoke(); }catch(TargetInvocationException ex){ LogError($"An error occurred:{ex}"); return default(TResult); } } } // Helper extension methods omitted for brevity... ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值