Tag Archives: .net

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>

Castle Project update

Hamilton has applied a bunch of patches and fixes to the Castle Project for the Components and Windsor sub-projects. Including 2 minor and insignificant patches that I submitted +D. It’s good to feel part of a major Open Source project, even for a small contribution.

http://hammett.castleproject.org/?p=336

While I’m currently focusing my efforts on ASP.NET MVC and moved away from MonoRail, I use Castle extensively in my .NET projects, particularly ActiveRecord and Windsor/MicroKernel.

Visual Studio Performance

I’m running Visual Studio 2008 on a old, single-core laptop with 1GB RAM.

VS is a resource hog, so it’s not long before my laptop is struggling having a reasonable size solution in addition to the usual open applications.

There are a few things you can do to try and squeeze a bit more performance out of Visual Studio I’ve found from various blogs and looking around myself. The first two are the most useful. Don’t muck with the other options unless you know what they’re going to do.

Recycle Memory
Perhaps the biggest trick is the simplest: if you minimize Visual Studio, it will force a garbage collection, and packing of the memory usage. This can make VS drop from a few hundreds MB of memory usage to 20 or less. Of course things will slow down when you go back into VS as things get reloaded into memory from virtual memory and disk.

Patch to SP1
Make sure Visual Studio 2005 SP1 is installed, for the bug and performance fixes since release.

Turn off animation
Tools > Options > Environment > General and uncheck Animate environment tools.

Disable the Navigation Bar
Go to Tools > Options > Text Editor > C# and uncheck Navigation bar under Display.

Turn off Track Changes
Tools > Options > Text Editor and uncheck Track changes.

Turn off Track Active item
Tools > Options > Projects and Solutions: uncheck Track Active Item in Solution Explorer.

Turn off AutoToolboxPopulate.
Tools > Options > Windows Forms Designer: set AutoToolboxPopulate to False.

Startup with an empty environment
Tools > Options > At startup: Show empty content

Windows Forms refactoring
Tools > Windows Forms Designer > General: Set “EnableRefactoringOnRename” to false in the Refactoring section

System.BadImageFormatException

I’m currently build the new Overclockers New Zealand website in .NET

I have a nice continuous integration server going smoothly, but my most recent check-in threw up a strange error when running the Unit Tests:

System.BadImageFormatException : Could not load file or assembly ‘Overclockers.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’ or one of its dependencies. The module was expected to contain an assembly manifest.

The server is actually a VMWare instance, but is running a 32-bit version of Windows Server 2003 EE.

After some troubleshooting and searching, I decided to first try to explicitly build for x86 first anyway. This is done in the Project Properties dialog in Visual Studio.

When changing this, make sure you make the change for both Debug and Release configurations.

This seemed to fix the problem.

I suspect this could be because I have some Shell32 interop in one of my shared libraries, and this may have caused problems, and/or the fact I’m running the instance on VMWare.