Running .NET 2 Runtime applications under the .NET 4 Runtime

In some situations, you might want to run a .NET 2 Runtime application (.NET 2, 3.0, 3.5 SP1 etc) under the .NET 4 Runtime – without recompiling.

You can configure your app to execute under the .NET 4 runtime by adding these lines to the executable’s configuration file, under the root  configuration element:

<startup>
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>

Because the security framework has changed in the .NET 4 runtime, you will likely encounter some exceptions containing a message similar to:

System.NotSupportedException: This method explicitly uses CAS policy, which has been obsoleted by the .NET Framework. In order to enable CAS policy for compatibility reasons, please use the NetFx40_LegacySecurityPolicy configuration switch. Please see http://go.microsoft.com/fwlink/?LinkID=155570 for more information.

To avoid this, you  have to enable the legacy support by adding a runtime element :

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

If your application also uses mixed assemblies that contain both managed and native code (such as System.Data.SQLite.dll), you’ll see an error message like this:

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.

You will need to enable the legacy activation to allow these to be loaded also:

<startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<runtime>
  <NetFx40_LegacySecurityPolicy enabled="true"/>
</runtime>

References

<startup> Element @ MSDN

<supportedRuntime> Element @ MSDN

<NetFx40_LegacySecurityPolicy> Element @ MSDN

<startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>
    <runtime>
        <NetFx40_LegacySecurityPolicy enabled="true"/>
    </runtime>

10 thoughts on “Running .NET 2 Runtime applications under the .NET 4 Runtime

  1. Anonymous

    < ![CDATA[Is the required even if all the assemblies (including the exe) are compiled using vs2005?]]>

    Reply
  2. Anonymous

    < ![CDATA[Hi,
    We have a MVC 3.0 app targeting framework 4.0 to be installed on IIS 6.0 on w2k3.

    The deployment is on a virtual directory that has target framework 4.0. The virtual directory belongs to a parent site that has framework 2.0 configured and there are existing apps targetting 2.0 framework. Is this the recommended deployment model or should the site be setup exclusively for framework 4.0 ?

    On deploying the app we are consistently getting the 403 error using IE client. The IIS log is as below

    2012-01-30 09:36:02 W3SVC1441322441 127.0.0.1 GET /TestMVC3App/ - 100 - 127.0.0.1 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.2;+SV1;+.NET+CLR+1.1.4322;+.NET+CLR+2.0.50727;+.NET+CLR+3.0.04506.30;+.NET+CLR+3.0.4506.2152;+.NET+CLR+3.5.30729;+.NET4.0C;+.NET4.0E) 403 14 5

    Installation steps followed were -

    - Framework 4.0

    - ran aspnet_regiis.exe with -ir (to retain 2.0)

    - changed virtual directory property to target v4.0.30319

    - added supportedFramework="v4.0.30319" in web.config

    Please let me know anything is missed out while setting up the environment.

    Also can you please le me know if we can have the Parent site and MVC3 app targetting 4.0 and rest of the apps which were targetting 2.0 will be left as it is?Is there any issue with this approach?

    Thanks in advance.
    --Nanju]]>

    Reply
  3. Nanjun

    Hi,
    We have a MVC 3.0 app targeting framework 4.0 to be installed on IIS 6.0 on w2k3.

    The deployment is on a virtual directory that has target framework 4.0. The virtual directory belongs to a parent site that has framework 2.0 configured and there are existing apps targetting 2.0 framework. Is this the recommended deployment model or should the site be setup exclusively for framework 4.0 ?

    On deploying the app we are consistently getting the 403 error using IE client. The IIS log is as below

    2012-01-30 09:36:02 W3SVC1441322441 127.0.0.1 GET /TestMVC3App/ – 100 – 127.0.0.1 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.2;+SV1;+.NET+CLR+1.1.4322;+.NET+CLR+2.0.50727;+.NET+CLR+3.0.04506.30;+.NET+CLR+3.0.4506.2152;+.NET+CLR+3.5.30729;+.NET4.0C;+.NET4.0E) 403 14 5

    Installation steps followed were –

    – Framework 4.0

    – ran aspnet_regiis.exe with -ir (to retain 2.0)

    – changed virtual directory property to target v4.0.30319

    – added supportedFramework=”v4.0.30319″ in web.config

    Please let me know anything is missed out while setting up the environment.

    Also can you please le me know if we can have the Parent site and MVC3 app targetting 4.0 and rest of the apps which were targetting 2.0 will be left as it is?Is there any issue with this approach?

    Thanks in advance.
    –Nanju

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>