Use msbuild 4.0 to target dotnet 3.5

.net-framework-versionmsbuild

In my solution I have project references to (e.g.) System.ServiceModel.dll where "use specific version" is set to false.
Everything builds fine with msbuild 3.5.

I'd like to use msbuild 4.0 in order to use property functions. I tried passing in properties "TargetFrameworkVersion=3.5;ToolsVersion=3.5". However, I get the following error:

Could not load file or assembly 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.configuration.dll' or one of its dependencies.

This doesn't happen if I use msbuild 3.5.

Can I make this work without specifying versions in the project files? (In future we may want to upgrade the application to dotnet 4)

Edit: I tried using msbuild 3.5 in the "outer" call, and invoking msbuild (using an<msbuild>task) 4.0 for those targets requiring the new functionality. No luck with this either – the inner invocation gives the following error message:

error MSB4062: Could not load file or assembly 'Microsoft.Build.Tasks.v4.0, Version=4.0.0.0 or one of its dependencies. The system cannot find the file specified. Confirm that the declaration is correct, and that the assembly and all its dependencies are available.

Best Answer

The parameter to pass to msbuild is:

/p:TargetFrameworkVersion="v3.5"

This is available in MSBuild version 3.5 and greater (3.5, 4.0, 2012, 2013, etc). It changes the property within the msbuild script for that build.

Extra note is that you could even specify all target versions installed on the machine for each of the MSBuild versions. Typically you can't do it in Visual Studio (e.g. use 4.5.1 in 2010), however MSBuild 4.0 (vs2010) will take it as a property value and use it.

Related Topic