MATLAB: Do I receive a runtime error when running an application that uses .NET 4.0 framework created with MATLAB Compiler 4.14 (R2010b)

2.04.0cannotcompileconfigframeworkloadMATLAB Compilernetnewer

I have a code that calls a .NET 4.0 DLL using NET.AddAssembly. This code works fine inside MATLAB. I am using MATLAB Compiler 4.14 to build a console application from the aforementioned code. I receive the following error at runtime
This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.

Best Answer

This issue occurs when the compiled executable tries to load the assembly with .NET 2.0 framework by default. In order to overcome this issue, create a CONFIG file. For example, if your executable is 'foo.exe', create a file called 'foo.exe.config'. Add the following contents to that file
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>
This will make sure .NET 4.0 is available when the application is running. The CONFIG file should be located in the same directory as the executable.