2010-07-05 62 views
34

大约两年前,我在.NET 2.0中编写了一个用于数据库访问的类库,并一直在.NET 2.0,3.0和3.5上使用它。.NET中的混合模式汇编4

在当前项目中我的工作(这是一个.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: 

有什么可能是错的,我该如何解决?

+0

我重新编译.NET 4.0的类库,但仍然没有奏效。 难道是因为我在类库中引用了SQLite.NET 1.0.65.0? – 2010-07-05 11:33:28

+0

重要提示:如果错误发生在“文件”错误列为“SGEN”,那么修复需要位于'sgen.exe'旁边的'sgen.exe.config'文件中。例如,对于VS 2015,创建'C:\ Program Files文件(x86)\ Microsoft SDKs \ Windows \ v10.0A \ bin \ NETFX 4.6 Tools \ sgen.exe.config'。资料来源:[SGEN混合模式程序集](https://support.microsoft.com/en-us/help/2572158/sgen-mixed-mode-assembly-is-built-against-version-v2-0-50727-of -the-ru)最小文件内容:'' – ToolmakerSteve 2017-10-06 01:25:41

回答

70

最好将可能重新编译类库.NET 4.0在Visual Studio 2010(即开放的项目,将其转换和改变目标框架。)

如果你不能,或不会这样做,那么您可以尝试将以下内容添加到您的.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> 
+0

在哪个位置我可以找到app.config? – DevThapa 2017-06-13 05:34:29

0

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

+0

我将类库升级到.NET 4,但它仍然无法工作。 我认为这是因为我在类库中引用了SQLite.NET。 – 2010-07-05 11:29:23

20

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

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

我希望你在看,但是如果混合模式程序集正在被不在你控制范围内的应用程序加载(比如说Excel.exe)会怎么样。应该在哪里提供配置信息? – ForeverLearning 2014-02-07 18:09:45

+3

在一个非常非常尴尬的地方,Excel.exe.config。任何人都可以搞砸它并杀死你的代码。有必要的时候考虑重建组件。 – 2014-02-07 18:12:55

+0

非常感谢您的确认。这确实是我要走的方向。 – ForeverLearning 2014-02-07 18:23:04

2

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

+3

但是这也会阻止库使用任何.Net 4.0类库功能和.net 4.0目标程序集。 – Deanna 2013-12-04 11:19:16

1

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

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

即使在安装兼容的安装程序后,如果它不工作,您必须删除对以前的DLL文件的引用,并添加对此兼容的DLL文件的新引用。

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

0

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

例如,如果您正在使用的x86 platorm针对.NET 4.5,你应该使用这个文件:

下预编译静态链接二进制32位Windows(.NET框架4。5)

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