38
votes

I wrote a class library in .NET 2.0 for database access some 2 years ago and have been using it all along on .NET 2.0, 3.0 and 3.5.

In the current project I'm working on (which is a .NET 4.0 application), tried using the old faithful class library and I got the following exception:


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: 

What could be wrong and how do I fix it?

7
I recompiled the class library for .NET 4.0 but is still did not work. Could it be because I reference SQLite.NET 1.0.65.0 in the class library?Alex Essilfie
IMPORTANT: If the error happens with error column "File" as SGEN, then the fix needs to be in a file sgen.exe.config, next to sgen.exe. For example, for VS 2015, create C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6 Tools\sgen.exe.config. Source: SGEN Mixed mode assembly Minimum file contents: <configuration><startup useLegacyV2RuntimeActivationPolicy="true"/></configuration>ToolmakerSteve

7 Answers

74
votes

The best would probably be to recompile your class library for .NET 4.0 in Visual Studio 2010 (ie. opening up the project, converting it, and changing the target framework.)

If you can't, or won't, do that, then you can try adding the following to your app.config file for your .NET 4.0 application:

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

ie.

<?xml version ="1.0"?> 
<configuration> 
    <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0"/>
    </startup>
</configuration>
23
votes

Since your assembly is mixed-mode, it potentially can call managed code from the unmanaged machine code in the assembly. With the new in-process side-by-side CLR version support in .NET 4.0, the runtime doesn't know which CLR version needs to be provided when that happens. You have to tell it that with an app.exe.config file that should look like this:

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

Another way: In VB 2010 Express you can open your project and go to the tab Compile and then Advanced Compile Options.... Select .NET Framework 2.0 from the drop-down list named Target framework (all configurations).

1
votes

If you are not getting through even by specifying useLegacyV2RuntimeActivationPolicy="true", you may need to install a setup that is compatible with your OS and .NET version. You can find the same at http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki.

Before installing this setup, uninstall SQLite from installed programs. That will show errors in your code.

If it is not working even after installing compatible setup, you have to remove the reference to your previous DLL file and add a new reference to this compatible DLL file.

When you add a reference to a compatible DLL file you just have to build your project and all your code errors should have gone.

0
votes

You could try opening the old faithful project (Class Library) inside of Visual Studio 2010 and allow it to do the conversion for you.

0
votes

If anyone else still run into this issue, here is my diagnosis: You are using wrong SQLite package. There are many versions of SQLite under http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

For example, if you are targeting .net 4.5 with x86 platorm you should use this file:

under Precompiled Statically-Linked Binaries for 32-bit Windows (.NET Framework 4.5)

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

0
votes

In my case, adding the following to the App.config fixed my issue:

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

Here's how it will look like:

<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
  </startup>
  <runtime>
    <loadFromRemoteSources enabled="true" />
  </runtime>
</configuration>