What do the TargetFramework settings mean in web.config in ASP .NET MVC?
Asked Answered
T

2

119

One of our ASP.NET MVC 5 web applications has the following web.config settings:

<system.web>
  <compilation debug="true" targetFramework="4.6" />
  <httpRuntime targetFramework="4.5" />
  <!--... many other things -->
</system.web>

It is not clear why are two targetFramework settings, and it seems to be wrong to compile targeting 4.6 then trying to run under 4.5...

Obviously I am missing something, but what?

Tympanic answered 21/10, 2016 at 7:16 Comment(1)
Related blog post blogs.msdn.microsoft.com/webdev/2012/11/19/…Cabbage
C
76

The reason of targetFramework existence in web.config is to keep compatibility issues out between breaking changes for each version of .NET Framework. The difference between targetFramework on compilation and httpRuntime belongs to each development and deployment environment.

According to MSDN blog:

<compilation targetFramework="4.6" />

Selects which version of the .NET Framework’s reference assemblies are used when performing compilation. (Note: Visual Studio requires that this element be present in Web.config, even though we auto-infer it.)

This element determines the assembly version used during compilation to create dependencies and related assemblies from current project.

<httpRuntime targetFramework="4.5" /> means that current project designed to use .NET 4.5 runtime assemblies without recompiling existing project assemblies in deployment machine before loading it into memory.

Hence, we can conclude that version number defined on targetFramework in httpRuntime element designed to maintain compatibility between compiled project and available assemblies in runtime usage, depending on which version of runtime files being used in target machine.

Thus, in your case, this is not a wrong behavior, the project creator(s) simply want to keep runtime compatibility to lowest runtime version available in target machine with similar characteristics (i.e. version 4.5), even the project compiled with newer version of .NET assemblies. The difference between version 4.5 and 4.6 is relatively small, thus keeping runtime version down to 4.5 still acceptable on this context.

Related references:

Clathrate answered 21/10, 2016 at 8:21 Comment(6)
Maybe it's me, I still do not understand how can something "...designed to use .NET 4.5..." when we used 4.6 when selected "...a version of the .NET Framework’s reference assemblies are used when performing compilation..."Tympanic
.NET 4.6 is a subset update of .NET 4.5, thus also based from .NET 4.5 assembly references with some minor differences (note that .NET has backward compatibility support since version 2.0). In fact, the project will compile using version 4.6 assembly references, but able to use version 4.5 runtime references on target machine when higher version ones are not available.Clathrate
You are referring "backward compatibility". However it means by definition: a thing which expects lower version, will function seamlessly in context of higher version. (because the higher version is backward compatible" Not the reverse.Tympanic
Found good article that can explain little more about it at msdn. blogs.msdn.microsoft.com/webdev/2012/11/19/…Ceraceous
This answer looks so wrong and the answerer has got it back to front; So if I use features in 4.6 and compile with it, when I deploy to the target server (4.5), the app should fail because said features do not exist in older version.Cartilaginous
@TetsuyaYamamoto i have one quick question if you agree that <httpRuntime targetFramework="4.5" /> basically gets translated to <compilation targetFramework="4.5" /> <machineKey compatibilityMode="Framework45" /> <pages controlRenderingCompatibilityVersion="4.5" /> and in that case explicit setting in web.config <compilation targetFramework="4.6" /> would be considered ?Lampkin
P
14

As per my understanding <compilation debug="true" targetFramework="4.6" /> is being suppressed by <httpRuntime targetFramework="4.5" /> since httpRuntime gets translated to following

<compilation targetFramework="4.5" />
<machineKey compatibilityMode="Framework45" />
<pages controlRenderingCompatibilityVersion="4.5" />

So above setting is probably due to some misunderstanding or a bug if done by VS directly which I don't believe is true.

To understand how this setting and all other related stuff means this blog titled All about <httpRuntime targetFramework> written by a Microsoft employee might help you. But the gist of it is;

The .NET Framework (including ASP.NET) strives to maintain near-100% compatibility when an existing framework is updated on a machine. We try to ensure as much as possible that if an application was developed and deployed against .NET Framework 4, it will just continue to work on 4.5. This normally means keeping quirky, buggy, or undesirable behaviors in the product between versions, as fixing them may negatively affect applications which were relying on those behaviors.

Pulsometer answered 3/5, 2018 at 2:2 Comment(6)
In the blog post it suggests that any settings that are explicitly set in the web.config will not get overridden by what is implied by the httpRuntime setting: "Even though <httpRuntime targetFramework="4.5" /> would normally imply <pages controlRenderingCompatibilityVersion="4.5" />, the runtime will notice that you have already explicitly set controlRenderingCompatibilityVersion and will respect your setting."Amenra
Just for information, if compilation is set while httpRuntime is not set, the machinekey and pages setting will not have 4.5 as default value, need to set them manually. This is quite important while trying to share cookie between legacy website with .net core website.Vorlage
what is the interpretation if the targetFramework is missing for both Compilation and HttpRuntime?Malacology
please confirm by google but I think these attributes are used to limit the scope of application and framework to a specific version however if not specified it would probably assume that there is no restriction and will try to run whatever the latest version installed. This might not be good idea though assuming you may have some apps running in old version e.g. 4.0 while you install another app which might be using the recent framework functionalities e.g. 4.7.2.Pulsometer
@Jarvan: if httpRuntime targetFramework is not set, default value is 4.0Responsibility
I assumed the Target framework setting in project files would determine the framework used during compilation of assemblies and that the setting in de web.config under compilation determines the version for compiling aspx, cshtml etc. However, I cannot find any documentation on this.Fluency

© 2022 - 2024 — McMap. All rights reserved.