12

I discovered that an app targeting 4.5 can load and run an assembly targeting 4.5.2 - but I can't compile such a setup!

Question: How can I compile a .NET 4.5 app with a reference to .NET 4.5.2 assembly?

Experiment details:

  • MyLib.csproj - Targets 4.5.2
  • MyApp.csproj - Targets 4.5, has project reference to MyLib.csproj

Compiling in either Visual Studio or MSBuild fails with:

warning MSB3274: The primary reference "(...)\MyLib.dll" could not be resolved because it was built against the ".NETFramework,Version=v4.5.2" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.5".
error CS0246: The type or namespace name 'MyLib' could not be found (are you missing a using directive or an assembly reference?)

I tried to compile MyLib separately and reference its DLL, but got the same compilation errors.

However, I managed to get both to run:

  • Set both projects to 4.5, compile, copy the result to c:\test\MyApp.exe
  • Set both projects to 4.5.2, compile, copy the result to c:\test\MyLib.dll
  • Run MyApp.exe - runs ok!

Background: I'm preparing a transition of our large NuGet-managed multi-project from .NET 4.5 to 4.5.2. The announcement says It is a highly compatible, in-place update to the .NET Framework 4, 4.5 and 4.5.1, So I tried what would happen if we upgraded some of our (NuGet-managed) refs to 4.5.2, and non-upgraded projects tried to consume them.

Jonathan
  • 6,939
  • 4
  • 44
  • 61
  • 5
    Why not just change the target framework of your app to 4.5.2? – rory.ap Feb 05 '15 at 16:37
  • 5
    Yes, it is a highly **backwards** compatible update. Not forward, they didn't release an update for 4.5 with all of the added types and members. So your app would start on machine with only 4.5 just fine, then shrivel up, keel over and die with a lousy exception when your code uses the non-existing members. Which is what the warning is telling you. Rather low odds that you are *actually* using such added members, there are few, so there wasn't any point in targeting 4.5.2. – Hans Passant Feb 05 '15 at 18:34
  • @roryap: The example is, of course, contrived. Our project is large, with over 1000 projects spanning multiple solutions, and many produce binaries that are distributed via NuGet to other solutions. So I'm trying to avoid upgrading ALL consumers of a certain binaries before I upgrade the binary itself. – Jonathan Feb 07 '15 at 13:48
  • Upgrade your entire project to 4.5.2. 4.5.2 is a binary *replacement* for 4.0 and 4.5. It's also the earliest supported .NET version, which means there's no point in targeting 4.5. The 4.5.2 assembly won't even work unless the machine already has 4.5.2 or higher installed – Panagiotis Kanavos Aug 25 '17 at 12:05
  • @PanagiotisKanavos : Isn't it possible to run application targeting 4.5.2 on machine having 4.5.1 ? (Provided application is not making any use of 4.5.2 specific feature) – rahulaga-msft Apr 10 '18 at 07:26
  • @RahulAgarwal where will you find such a machine *in 2018*? Windows Update probably patched it long ago. And why use an *unsupported* point version anyway? As for `not making any use of 4.5.2 specific features` there are probably none that matter. The API changes like `async/await`, the addition of TLS 1.2 came with 4.5 – Panagiotis Kanavos Apr 16 '18 at 08:01

2 Answers2

3

For backwards compatible you can use multi-targeting. Open your .csproj file and change it like this:

 <PropertyGroup>
    <TargetFrameworks>net462;netstandard1.6</TargetFrameworks>
    <PreserveCompilationContext>true</PreserveCompilationContext>
  </PropertyGroup>

This is an example for .nuget package for two frameworks: net462 and netstandard1.6

At building it will create two folders with libraries:

  • net462
  • netstandard1.6
Timur Lemeshko
  • 2,747
  • 5
  • 27
  • 39
0

You can try to make your library a .NET Standard library rather than .NET Framework 4.X one.

See the related thread.

Dmitry Pavlov
  • 30,789
  • 8
  • 97
  • 121
  • Easier to upgrade the project. 4.5 isn't even supported any more. The earliest supported vesion is 4.5.2. Besides, to get a .NET Standard library to run, one would have to upgrade the runtime to a supported version anyway – Panagiotis Kanavos Aug 25 '17 at 12:06
  • @PanagiotisKanavos sure, but it depends on what is required. – Dmitry Pavlov Aug 25 '17 at 16:43