9

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?

David Mills
  • 2,385
  • 1
  • 22
  • 25

1 Answers1

12

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.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Thanks, that blog post appears to describe the exact problem we're experiencing. However, as far as we can tell the build server *does* already have the 4.0 reference assemblies, so we're still not sure what's going wrong. – David Mills Feb 12 '13 at 21:48
  • @DavidMills In general, I'd actually strongly suggest not installing 4.5 on a build server if you are going to target 4.0. There are just too many breaking changes in this in-place upgrade. That being said, it should work if the ref assemblies are in the correct location. – Reed Copsey Feb 12 '13 at 22:20
  • Thanks, Reed. We've gone ahead and set up a Jenkins build slave server to support projects targeting 4.0. It's unfortunate, but seemingly necessary. Hopefully Microsoft will think twice before releasing another .NET version as an in-place upgrade. – David Mills Feb 12 '13 at 22:28
  • "Hopefully Microsoft will think twice before releasing another .NET version as an in-place upgrade"... Hello .NET 4.5.1! – Danny Tuppeny Jul 01 '13 at 08:58
  • 1
    Your answer is causing some [unnecessary angst](http://stackoverflow.com/questions/25555601/how-do-you-add-the-4-0-reference-assemblies-to-your-build-server-so-the-compile), you might want to annotate a bit. – Hans Passant Aug 28 '14 at 19:29