39

大约 2 年前,我在 .NET 2.0 中编写了一个用于数据库访问的类库,并且一直在 .NET 2.0、3.0 和 3.5 上使用它。

在我正在处理的当前项目(这是一个 .NET 4.0 应用程序)中,尝试使用旧的忠实类库,但出现以下异常:


System.InvalidOperationException 未处理
  Message=创建表单时出错。有关详细信息,请参阅 Exception.InnerException。
    错误是:混合模式程序集是针对运行时版本“v2.0.50727”构建的
    并且在没有额外配置信息的情况下无法在 4.0 运行时加载。
    来源=学校经理
  堆栈跟踪:
       在 SchoolManager.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190
       在 SchoolManager.My.MyProject.MyForms.get_frmGeneric()
       在 D:\Alex\Documents\Visual Studio 2008\Projects\School Manager\SchoolManager\My Project\Application.Designer.vb:line 35 中的 SchoolManager.My.MyApplication.OnCreateMainForm()
       在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(字符串 [] 命令行)
       在 SchoolManager.My.MyApplication.Main(String[] Args) 在 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       在 System.AppDomain._nExecuteAssembly(RuntimeAssembly 程序集,字符串 [] 参数)
       在 System.AppDomain.ExecuteAssembly(字符串 assemblyFile,证据 assemblySecurity,String [] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态,布尔 ignoreSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态)
       在 System.Threading.ThreadHelper.ThreadStart()
  内部异常:System.IO.FileLoadException
       Message=混合模式程序集是针对版本“v2.0.50727”构建的
           运行时,并且无法在 4.0 运行时中加载而无需额外的
           配置信息。
       来源=Dinofage.Data.XpressData
       堆栈跟踪:
            在 Dinofage.Data.XpressData.ExecuteSelectInternal(字符串选择命令)
            在 Dinofage.Data.XpressData.ExecuteSelect(字符串选择命令)
            在 D:\Alex\Documents\Visual Studio 2008\Projects\School Manager\SchoolManager\Modules\Academics.vb 中的 SchoolManager.Academics.GetNewAdmissionCode(String AcademicYear):第 89 行
            在 D:\Alex\Documents\Visual Studio 2008\Projects\School Manager\SchoolManager\UserControls\StudentDetail.vb:line 20 中的 SchoolManager.StudentDetail..ctor()
            在 D:\Alex\Documents\Visual Studio 2008\Projects\School Manager\SchoolManager\frmGeneric.Designer.vb:line 25 中的 SchoolManager.frmGeneric.InitializeComponent()
            在 SchoolManager.frmGeneric..ctor()
       内部异常:

可能出了什么问题,我该如何解决?

4

7 回答 7

74

最好的方法可能是在 Visual Studio 2010 中为 .NET 4.0 重新编译您的类库(即打开项目、转换它并更改目标框架。)

如果您不能或不会这样做,那么您可以尝试将以下内容添加到您的 .NET 4.0 应用程序的 app.config 文件中:

<startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
</startup>

IE。

<?xml version ="1.0"?> 
<configuration> 
    <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0"/>
    </startup>
</configuration>
于 2010-07-05T11:24:06.417 回答
23

由于您的程序集是混合模式,因此它可能会从程序集中的非托管机器代码中调用托管代码。借助 .NET 4.0 中新的进程内并行 CLR 版本支持,运行时不知道发生这种情况时需要提供哪个 CLR 版本。您必须使用应该如下所示的 app.exe.config 文件来告诉它:

<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
  </startup>
</configuration>
于 2010-07-05T11:33:22.537 回答
2

另一种方式:在 VB 2010 Express 中,您可以打开项目并转到选项卡 Compile,然后Advanced Compile Options.... .NET Framework 2.0从名为 的下拉列表中选择Target framework (all configurations)

于 2012-11-08T14:10:47.463 回答
1

如果即使指定 useLegacyV2RuntimeActivationPolicy="true" 也没有通过,您可能需要安装与您的操作系统和 .NET 版本兼容的设置。您可以在http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki找到相同的内容。

在安装此设置之前,请从已安装的程序中卸载 SQLite。这将在您的代码中显示错误。

如果即使在安装了兼容的设置后仍然无法正常工作,您必须删除对以前的 DLL 文件的引用并添加对这个兼容的 DLL 文件的新引用。

当您添加对兼容 DLL 文件的引用时,您只需构建您的项目,所有代码错误都应该消失。

于 2014-01-20T11:27:41.997 回答
0

您可以尝试在 Visual Studio 2010 中打开旧的忠实项目(类库)并允许它为您进行转换。

于 2010-07-05T11:19:53.113 回答
0

如果其他人仍然遇到此问题,这是我的诊断:您使用了错误的 SQLite 包。http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki下有很多SQLite版本

例如,如果您的目标是 .net 4.5 和 x86 platorm,您应该使用这个文件:

在 32 位 Windows 的预编译静态链接二进制文件 (.NET Framework 4.5) 下

sqlite-netFx45-static-binary-bundle-Win32-2012-1.0.106.0.zip

于 2017-11-08T15:51:31.903 回答
0

就我而言,将以下内容添加到已App.config修复的问题中:

<runtime>
  <loadFromRemoteSources enabled="true" />
</runtime>

下面是它的样子:

<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
  </startup>
  <runtime>
    <loadFromRemoteSources enabled="true" />
  </runtime>
</configuration>
于 2021-05-04T20:01:01.110 回答