0

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?
johnfound
  • 6,857
  • 4
  • 31
  • 60
faronel
  • 333
  • 1
  • 9
  • Why do you install the library in GAC in the first place? – galenus Oct 23 '13 at 12:58
  • This just isn't going to work, you really *do* have to target .NET 4.5 to be able to use that version of Newtonsoft.Json.dll. Targeting .NET 3.5 is where this went wrong. Earlier versions of that assembly are available as well but then you still won't be able to use that Logger assembly that wants to use the 4.5 version. Targeting 3.5 requires that *all* assemblies you use can run on the v2 CLR. Messing with the GAC is not a workaround. – Hans Passant Oct 23 '13 at 12:59
  • @HansPassant The first version of the assembly he refers to seems to target 3.5 (last line!). If I'm not mistaken then, that should work fine, as long as he manages to make his application refer to the correct version. – Kjartan Oct 23 '13 at 13:04
  • `MyApp` and `Logger` have target framework `.NET Framework 3.5` and I want to use `Newtonsoft.Json.dll` compiled against `.NET Framework 3.5`. The `Newtonsoft.Json.dll` of target framework 4.0 in the GAC of .NET 4.0 is a scenario that happens when an installer (of some other application compiled against .NET 4.0 and using Json) installs the Json library in the GAC 4.0 – faronel Oct 23 '13 at 13:17

1 Answers1

0

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.

Kjartan
  • 18,591
  • 15
  • 71
  • 96
  • Both libraries have `Version 4.5.0.0`. This is the version information found by reflector and this version information is also presented by visual studio. What differs is the `AssemblyFileVersion` but visual studio seems to ignore this. – faronel Oct 23 '13 at 13:12
  • @faronel Ah, Ok. I didn't notice that in you OP. I find it a little strange though, that there are different versions of `Newtonsoft.Json.dll` with the same version number (how can they _not_ be different, when they target different version of the .Net framework?). I assume the `Newtonsoft` dll is not something you compile yourself? (if so, you could simply create different versions of it yourself, to solve this) – Kjartan Oct 23 '13 at 13:21
  • 1
    The `Newtonsoft.Json.dll` libraries are not compiled by myself. The version with target framework 3.5 is downloaded from http://json.codeplex.com/ and is located in the zip file under `/Bin/Net35/Newtonsoft.Json.dll` Compiling the library by myself with another version number is an alternative. But I'm wondering why this does not work since my target framework is 3.5 and the one of the GAC library is 4.0. – faronel Oct 23 '13 at 13:26
  • Ok. I suppose VS simply "assumes" there is no difference, since the version number is the same, and therefore refers you to the GAC instead... – Kjartan Oct 23 '13 at 13:30