Crash when running a .NET 3.5 app on .NET 4

2019-07-24 14:49发布

问题:

Our application is built using WinForms on .NET 3.5

This was tested specifically on Windows XP, although i am not sure whether it is OS related.

Whenever i run it on a machine with .NET 4 only, it will crash, saying that it cannot find the .NET assemblies versioned 3.5.

I am wondering why this does not happen automatically. For example, some 3rd party libraries install a policy of some sort, that when a newer version is installed, it will be used, even if your application was compiled against an older version.

UPDATE: The exact error message is:

"Unable to find a version of the runtime to run this application".

My questions are:

  1. Why is this not the same with the .NET framework?
  2. The solution is to add the element in the configuration file? Any other solutions?

回答1:

If your computer has no .NET 3.5 installed there is no CLR to start for your app. .NET 4.0 is not automatically used for your app because of potential compatibility issues. First test that your app does run with .NET 4.0 and then add this section to your app.config to tell the CLR to prefer running .NET 4.0 if present.

<configuration>
   <startup>
      <supportedRuntime version="v4.0.30319"/>
   </startup>
</configuration>

If .NET 4 is not present as fallback the CLR version against which you application was compiled against is used as fallback. If all fails you get the message "Unable to find a version of the runtime to run this application".



回答2:

In general, that should not be the case:

http://msdn.microsoft.com/en-us/library/ff602939.aspx

Backward compatibility means that an application developed for a particular version of a platform will run on later versions of that platform. The .NET Framework tries to maximize backward compatibility: Source code written for one version of the .NET Framework should compile on later versions of the .NET Framework, and binaries that run on one version of the .NET Framework should behave identically on later versions of the .NET Framework.

...

The .NET Framework 4 is backward-compatible with applications that were built with the .NET Framework versions 1.1, 2.0, 3.0, and 3.5. In other words, applications and components built with previous versions of the .NET Framework will work on the .NET Framework 4.

SUGGESTIONS:

1) Read these links:

  • http://msdn.microsoft.com/en-us/library/ff602939.aspx

  • http://msdn.microsoft.com/en-us/library/dd889541.aspx

  • http://msdn.microsoft.com/en-us/library/ee461502.aspx

2) See if you can reproduce the problem with a minimal .Net 3.5 Winforms app

3) Cut/paste the exact error message, and any relevant code (e.g. from your standalone app)

4) Failing all else, consider adding <startup useLegacyV2RuntimeActivationPolicy="true" > to your CLR startup configuration. I would not recommend this as anything but a temporary workaround:

  • http://msdn.microsoft.com/en-us/library/bbx34a2h.aspx


回答3:

.NET 4.0 introduce new CLR. So basicly having 4.0 doesn't help much in running 3.5 apps. It has been already mentioned here