Application built with .NET 3.5 running on machine with only .NET 4.0. How does supportedRuntime element work? - .net-4.0

We have an application that was built using .NET 3.5. There is a situation where it will run on a machine which only has .NET 4.0 installed.
If in the application configuration file the <supportedRuntime> element is not defined, or is defined as follows
<startup>
<supportedRuntime version="v2.0.50727"/>
</startup>
will the application crash on start up since .NET 3.5 and CLR v2.0.50727 are not present?
Note I understand that .NET 4.0 APIs are supposed to be compatible with .NET 3.5 ones and that a .NET 3.5 application should run in .NET 4.0. That is not the question. I am specifically asking about the <supportedRuntime> behavior.

Adding Hans Passant's comment as it seems a suitable answer.
The application will crash. To run on .NET 4.0 you need to add another supportedRuntime attribute with version="4.0".

Related

Can we use .Net 4.0 framework in the Visual Basic Interop form?

I'm having a project which is to add new features to a VB6 application. In order to migrate the application to the more advanced technology incrementally, we choose to use VB Interop technology. However, we found that the VB Interop Form is actually based on .Net 2.0. So, I'm asking how can we use higher version of the .Net framework?
thanks
Short answer is no, but stable in 3.5 framework. Microsoft is aware of the issue, but can't tell you when there would be a fix. You can find more here.
Another good read here.
Interop Forms Toolkit can be used with .NET 4.0. I have been using it for more than 6 months with no problems.
The problem discussed here is due to In-Process Side-by-Side Execution, introduced in .NET 4. With this you can have different versions of the CLR running in your application. This can be fixed by using an app.config file:
<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
</configuration>
I answered a similar problem here, and here

Assembly.LoadFrom BadImageFormatException - different behavior in .NET 4.0 and 4.5 (possibly undocumented)

According to MSDN documentation,
public static Assembly LoadFrom(string assemblyFile)
throws BadImageFormatException if
assemblyFile is not a valid assembly.
-or-
Version 2.0 or later of the common language runtime is currently loaded
and assemblyFile was compiled with a later version.
Actually, there is one extra case - loading assembly that is built for x86 from assembly that runs in x64 mode. Maybe it is included in "not a valid assembly" statement, I don't know. But this is reasonable cause of exception.
Ok, but in .NET 4.5 it doesn't! I have a .NET 4.5 WPF app, that loads different appliations for some reason. It is building for Any CPU and I'm starting it on x64 Win 7. I've been testing it on one executable, that is built for .NET 4.0 x86, and it worked fine. But when I switched my app to .NET 4.0 it began to crash on Assembly.Load method!
So, my question is, am I missing something? If not, then how did they do that - loading x86 assembly from x64 process in .NET 4.5? I'm lacking some understanding at this point.
Update
Thanks to Hans Passant, I've figured out my mistake. Actually the behavior of Assembly.Load is no different. It turned out, I didn't notice Prefer 32-bit option in project settings (or Prefer32Bit tag in .csproj file). That's why my process in .NET 4.5 ran in a 32-bit mode. This setting was true when I created WPF .NET 4.5 project. Then, when I swithced to .NET 4.0 it became inactive because there was no such an option in .NET 4.0. And when I switched back to .NET 4.5 it became false, which is so, I guess, for compatibility purpose.
Let's clear one assumption off the table quickly, there is no possible way to have different behavior on a machine that has .NET 4.5 installed. Targeting 4.0 makes no difference at runtime. The only thing that does is select a different set of reference assemblies, they prevent you from accidentally using a class that's available on .NET 4.5 but not on .NET 4.0.
There is no way to have both 4.0 and 4.5 installed on the same machine. .NET 4.5 is not a side-by-side version of the .NET framework, like 3.5 and 4.0 are side-by-side. Installing 4.5 replaces an installed 4.0 version. The CLR, the jitter, all the runtime assemblies plus the C# compiler.
It is best here to focus on the Platform target setting of your EXE project, that's the one that selects the bitness of the process. The kind of mistakes you can make is forgetting that the setting can be different for the Debug vs the Release build. And assuming that the "Active solution platform" combobox in Build + Configuration Manager has any effect. It doesn't, only the Project + Properties, Build tab, Platform target setting matters. This is a very awkward trap that many programmers have fallen into.

Is it possible to deploy a .NET 4.0 to a machine with only .NET 3.5 SP1 installed?

I am working on a WPF application that requires .NET 4.0 to run. The machines on the enterprise are all running .NET 3.5 SP1. Is it possible to deploy the application with its .NET 4 DLLs without having to install the full .NET 4 Framework on the client machines?
There are two parts of our implementation marries us to .NET 4.0
The use of the Data Grid user control (this is the easiest issue to overcome).
The use of the enableUnsecuredResponse property for my WCF binding.
<bindings>
<customBinding>
<binding name="CadsBinding">
<textMessageEncoding messageVersion="Soap11"/>
<security enableUnsecuredResponse="true" authenticationMode="UserNameOverTransport" allowInsecureTransport="true">
<secureConversationBootstrap/>
</security>
<httpTransport authenticationScheme="Basic"/>
</binding>
</customBinding>
</bindings>
Microsoft has release the following Hot Fix: A hotfix that enables WCF to send secured messages and to receive unsecured responses, and to send unsecured messages and to receive secured responses, is available for the .NET Framework 3.5 SP1 but if I am to deploy this Hot Fix to the enterprise, I might as well do that for the .NET 4.0 Client Profile and resolve all my issues then and there.
I am aware of the .NET 4.0 Client Profile installation. What I'm trying to avoid is the need to push out the installation of the framework for just my application.
My Current Thought:
It can't be done. Even if I deploy the .NET 4 DLLs that are referenced, they will still try to be handled by an older version of the CLR which won't be able to understand them.
You are going to need .NET 4, but there is the Client Profile
The .NET Framework 4 Client Profile is a subset of the .NET Framework 4 that is optimized for client applications. It provides functionality for most client applications, including Windows Presentation Foundation (WPF), Windows Forms, Windows Communication Foundation (WCF), and ClickOnce features. This enables faster deployment and a smaller install package for applications that target the .NET Framework 4 Client Profile.
This is a smaller download and smaller footprint version of the framework.
No. Those DLLs depend on assemblies registered in the GAC. Without .Net framework installed you don't get those assemblies. Why not just deploy with an installer?
No it is not possible to deploy an application which was compiled against 4.0 to a machine which only contains .Net 3.5 (any version). The 4.0 version of the runtime must be installed on the machine.
However it is possible to use Visual Studio 2010 (and 2008) to compile an assembly targeting 3.5. This would then be deployable to a machine only having the 3.5 framework. This feature is known as multi-targeting and here is a link to a tutorial on the subject
http://msdn.microsoft.com/en-us/library/bb398197.aspx
it depends on what features from 4.0 you use... some of them are just syntatic sugar and the compiler generates IL for that. For example, the new "dynamic" keyword uses the new Dynamic Language Runtime (DLR), which is only available as part of .net 4.0. So, if your app uses that, there's no other way than to install .net framework 4.0 in the machine
For me, the best idea would be to set in the project properties the target version to 3.5 (specified in the properties for your project in Visual Studio), and if there aren't any errors, you can follow this general
guidelines to test whether the app will work or not.

Upgrading a .Net 2.0 project to .Net 4.0

I have a .Net 2.0 project that depends on many 3rd party .Net dlls (all of which obviously target .Net 2.0).
If I were to migrate my project to VS2010 and target the .Net 4.0 framework, will my app still build? Or will it complain about the .Net 2.0 dll references and I will have to find .Net 4.0 versions of these 3rd party dlls?
Yes it will work. Make sure that you have both the .NET 2 and 4 FW installed on the machines executing the application.
If you need to use older assemblies with 4.0 (Mixed-Mode) you may need to add the following to <yourappname>.config:
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
I had to do this when I attempted to load some old 1.1 assemblies into my Ironpython program (.NET 4.0) and got the following error:
"Mixed mode assembly is built against version 'v1.1.4322' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information."
Adding those three lines to my ipyw.exe.config file let me run those assemblies in mixed mode.

How can I target .Net 4.0 Beta using NAnt?

I want to start testing my project using the Microsoft .Net 4.0 Beta version that has already been released. I know that adding a "net-4.0" target framework to NAnt requires updating nant.exe.config file, does anyone know what are the necessary changes?
If you want to use nant to build projects targeting .NET 4.0 you'll have to modify NAnt.exe.config and add the net-4.0 target framework and add a <supportedRuntime ... /> line to the <startup> section.
http://paigecsharp.blogspot.com/2009/08/nant-net-framework-40-configuration.html is a full code for .config file for NAnt.