Visual Studio uses assembly from newer target framework

2.3k views Asked by At

I am developing against target framework .NET Framework 3.5. A project Logger uses the following library

Newtonsoft.Json.dll
Version = 4.5.0.0
AssemblyFileVersion = 5.0.8.16617
PublicKeyToken = 30ad4fe6b2a6aeed
TargetFramework = v3.5

which is a referenced assembly in the project.

An application MyApp has a project reference to the Logger project and so an indirect reference to Newtonsoft.Json.dll.

Setting the Copy Local property to true will enforce Newtonsoft.Json.dll to be copied to the output directory. Compiling the MyApp will also copy the Newtonsoft.Json.dll into the corresponding output directory. Note that there is no Newtonsoft.Json.dll in the GAC

Fine so far.

Now I installed the following library to the .NET Framework 4.0 GAC (%windir%\Microsoft.NET\assembly):

Newtonsoft.Json.dll
Version = 4.5.0.0
AssemblyFileVersion = 4.5.8.15203
PublicKeyToken = 30ad4fe6b2a6aeed
TargetFramework = v4.0

Having this library installed in the GAC for .NET Framework 4.0 will cause the project referenced Newtonsoft.Json.dll is no longer copied to the output directory of MyApp. But it is still copied to the output directory of Logger.

I tried to explain this behavior to myself: The project referenced Newtonsoft.Json.dll is copied to the output directory of Logger because it's Copy Local property is set to true.

But I do not get why the Newtonsoft.Json.dll is no more copied to the output directory of MyApp. The library I installed to the GAC of .NET Framework 4.0 should (in my opinion) not be 'visible' to the build system, since my project is built against .NET Framework 3.5.

At runtime there will be a System.IO.FileNotFoundException because the Newtonsoft.Json.dll library is not found (this is clear to me, because the library is located in the 4.0 GAC which is not the GAC 3.5 used by MyApp).

  • Why does the build system of a target framework 3.5 project knows about assemblies in the GAC of .NET Framework 4.0?
  • How do I get the local referenced Newtonsoft.Json.dll with target framework 3.5 be copied to the output directory of MyApp when there is a Newtonsoft.Json.dll in the GAC 4.0?
1

There are 1 answers

4
Kjartan On

Have you tried setting Specific version to true under properties for the reference? I suspect that the GAC will be searched first during compilation, so if you don't specify a version, any version will be considered "acceptable", and no other version will be exported.

If you specify a version, that from the GAC should not be considered valid, and the other one should be exported, as before.

Update: I did a quick search, and discovered that the Newtonsoft package seems to be released under an MIT liscence, which effectively means you can do whatever you want with it.

A solution for you might therefor be to compile it yourself, with whatever target framework and version number you need.