.Net Application – Execution Error – Mixed mode assembly is built against version ‘v2.0.50727’ of the runtime and can not be loaded in the 4.0 runtime without additional configuration information”

 

Error Message


Exception caught in Main()
---------------------------------------
Mixed mode assembly is built against version 'v2.0.50727' of the runtime and can
not be loaded in the 4.0 runtime without additional configuration information.

Fix

To fix add or modify app.config as follow.

Add/Modify App.Config

Explanation

  1. Add startup element
    • Add attribute useLegacyV2RuntimeActivationPolicy=”true”

 

Code

<?xml version ="1.0"?>
<configuration>

  <startup useLegacyV2RuntimeActivationPolicy="true" >
  
  </startup>
  
</configuration> 

BUILD / DEPLOY

Please make sure to copy app.config to [application].exe.config.

A sample compile and deploy file is pasted below.



set "DIR_COMPILER=C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319"
set "REF_DLL_LIST=output\Microsoft.SqlServer.Smo.dll, output\Microsoft.SqlServer.ConnectionInfo.dll"

set ConnectionInfo="D:\Program Files\Microsoft SQL Server\120\SDK\Assemblies\Microsoft.SqlServer.ConnectionInfo.dll"
set Smo="D:\Program Files\Microsoft SQL Server\120\SDK\Assemblies\Microsoft.SqlServer.Smo.dll"
set SdkSfc="D:\Program Files\Microsoft SQL Server\120\SDK\Assemblies\Microsoft.SqlServer.Management.Sdk.Sfc.dll"
set SqlEnum="D:\Program Files\Microsoft SQL Server\120\SDK\Assemblies\Microsoft.SqlServer.SqlEnum.dll"

set "APPNAME=bin\Debug\ScriptDB.exe"
set "APPCONFIG_BASE=app.config"
set "APPCONFIG=%APPNAME%.config"

If not exist "bin\Debug" md "bin\Debug"

if exist %APPNAME% del %APPNAME%

%DIR_COMPILER%\csc /reference:%ConnectionInfo%,%Smo%,%SdkSfc%,%SqlEnum% /define:DEBUG /optimize  /out:%APPNAME% *.cs

if exist %APPCONFIG_BASE% xcopy /Y /D %APPCONFIG_BASE% %APPCONFIG%

Credit

Crediting StackOverflow

  1. Mixed mode assembly is built against version ‘v2.0.50727′ of the runtime

 

What is useLegacyV2RuntimeActivationPolicy?

Microsoft’s WriteUp

Here is Microsoft’s write-up

useLegacyV2RuntimeActivation

In Microsoft .Net v4.0 and later, legacy runtime activation is not allowed.

 

What Framework is SQL Server Management Objects (SMO) Compiled Against?

What .Net Framework is the SQL Server Management Object (SMO)  Version 9 compiled against?

One way to find out is to use a .Net Decompiler.

.Net Decompilers

There are a couple of .Net Decompilers.

  1. JetBrains
  2. Telerik

 

JetBrains – DotPeek

Pick DLL

PickSQLServerSMO

 

Assembly Explorer

AssemblyExplorer-Use

Explanation:

  1. We can see the SQL Server SMO ( v9) is compiled against .Net Framework v3.5

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s