代码之家  ›  专栏  ›  技术社区  ›  Alex Essilfie

.NET4中的混合模式程序集

  •  36
  • Alex Essilfie  · 技术社区  · 15 年前

    大约两年前,我在.NET2.0中为数据库访问编写了一个类库,并一直在.NET2.0、3.0和3.5中使用它。

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


    System.InvalidOperationException was unhandled
      Message=An error occurred creating the form. See Exception.InnerException for details.
        The error is: Mixed mode assembly is built against version 'v2.0.50727' of the runtime
        and cannot be loaded in the 4.0 runtime without additional configuration information.
        Source=SchoolManager
      StackTrace:
           at SchoolManager.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190
           at SchoolManager.My.MyProject.MyForms.get_frmGeneric()
           at SchoolManager.My.MyApplication.OnCreateMainForm() in D:\Alex\Documents\Visual Studio 2008\Projects\School Manager\SchoolManager\My Project\Application.Designer.vb:line 35
           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
           at SchoolManager.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException: System.IO.FileLoadException
           Message=Mixed mode assembly is built against version 'v2.0.50727' of 
               the runtime and cannot be loaded in the 4.0 runtime without additional
               configuration information.
           Source=Dinofage.Data.XpressData
           StackTrace:
                at Dinofage.Data.XpressData.ExecuteSelectInternal(String selectCommand)
                at Dinofage.Data.XpressData.ExecuteSelect(String selectCommand)
                at SchoolManager.Academics.GetNewAdmissionCode(String academicYear) in D:\Alex\Documents\Visual Studio 2008\Projects\School Manager\SchoolManager\Modules\Academics.vb:line 89
                at SchoolManager.StudentDetail..ctor() in D:\Alex\Documents\Visual Studio 2008\Projects\School Manager\SchoolManager\UserControls\StudentDetail.vb:line 20
                at SchoolManager.frmGeneric.InitializeComponent() in D:\Alex\Documents\Visual Studio 2008\Projects\School Manager\SchoolManager\frmGeneric.Designer.vb:line 25
                at SchoolManager.frmGeneric..ctor()
           InnerException: 
    

    有什么问题,我该怎么解决?

    6 回复  |  直到 15 年前
        1
  •  74
  •   Lasse V. Karlsen    15 年前

    最好的方法可能是在VisualStudio2010中重新编译.NET4.0类库(即打开项目、转换项目和更改目标框架)

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

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

    <?xml version ="1.0"?> 
    <configuration> 
        <startup useLegacyV2RuntimeActivationPolicy="true">
            <supportedRuntime version="v4.0"/>
        </startup>
    </configuration>
    
        2
  •  22
  •   Hans Passant    15 年前

    由于程序集是混合模式,因此它可能会从程序集中的非托管计算机代码调用托管代码。由于.NET4.0中新的进程内并行CLR版本支持,运行时不知道需要提供哪个CLR版本。您必须告诉它,对于app.exe.config文件,应该如下所示:

    <configuration>
      <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0"/>
      </startup>
    </configuration>
    
        3
  •  2
  •   Nicos Karalis    13 年前

    另一种方法:在vb2010express中,你可以打开你的项目,进入Compile选项卡,然后单击Compile Advanced Compile Options... .NET Framework 2.0 从名为 Target framework (all configurations) .

        4
  •  1
  •   Kishor Pawar    8 年前

    如果即使指定useLegacyV2RuntimeActivationPolicy=“true”也无法通过,则可能需要安装与操作系统和.NET版本兼容的安装程序。你可以在 http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

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

    如果安装兼容安装程序后仍无法工作,则必须删除对以前DLL文件的引用,并向此兼容DLL文件添加新引用。

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

        5
  •  0
  •   Peter Mortensen Pieter Jan Bonestroo    8 年前

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

        6
  •  0
  •   Cenk    8 年前

    如果还有人遇到这个问题,我的诊断是: SQLite有很多版本 http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

    例如,如果使用x86 platorm将.net 4.5作为目标,则应使用以下文件:

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

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

        7
  •  0
  •   Eli Nunez    5 年前

    在我的例子中,在 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>
    
    推荐文章