.net – Can a build server with .NET 4.5 installed successfully deploy a project targeting 4.0 to a server with only .NET 4.0 installed

.net.net-4.0.net-4.5.net-framework-version

We've recently installed .NET 4.5 onto our continuous integration build server so that it can support new projects that utilize features of .NET 4.5. This build server is also used to build and deploy older projects, as well, some of which target .NET 4.0.

Projects that target .NET 4.0 being built on this server, then deployed to a target server that has only .NET 4.0 installed are now failing with the following error:

Method not found: 'Int32 System.Environment.get_CurrentManagedThreadId()'.

Environment.CurrentManagedThreadId is a new property of .NET Framework 4.5, so it makes sense that a server running 4.0 can't find it. However, we are targeting .NET 4.0 at build time, so in theory we shouldn't need to have 4.5 installed on the production server.

To sum up:

  • Project targets 4.0
  • Build server has 4.5 installed
  • The server on which the project is then deployed has only .NET 4.0
  • Project fails at runtime with error Method not found: 'Int32 System.Environment.get_CurrentManagedThreadId()'.

What gives? Is it possible to successfully run .NET 4.0 dlls on a server with only .NET 4.0 installed when the dlls are built by a server with .NET 4.5?

Best Solution

This occurs because 4.5 is an in-place upgrade to 4.0. When the build server compiles, by default, it will find the 4.5 assemblies even if you're targeting .NET 4.

You can correct this, but you need to add the 4.0 reference assemblies to your build server (so the compiler finds them), and not just rely on the .NET 4.5 versions.

For details, see Marc Gravell's blog post on the subject.