23

I have the following object:

public class ProjectInfo
{
    public string ConnectionStringName { get; set; }
    public string DefaultEntityNamespace { get; set; }
    public string DefaultSharedNamespace { get; set; }
    public string DefaultTestNamespace { get; set; }
    public string SqlProviderName { get; set; }
}

Which I try to do a simple serialization of (in a VSIX project):

var settings = new ProjectInfo { ConnectionStringName = "SomeName" };
var json = JsonConvert.SerializeObject(settings);

which gives me:

An exception of type 'System.IO.FileNotFoundException' occurred in Newtonsoft.Json.dll but was not handled in user code

Additional information: Could not load file or assembly 'System.Xml.Linq, Version=5.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

I've spent the last hour trying to figure out where the dependency comes from or why Json.NET tries to use that namespace. System.Xml.Linq is not referenced in any of my projects.

From the stack trace I can see:

   at Newtonsoft.Json.Converters.XmlNodeConverter.CanConvert(Type valueType)
   at Newtonsoft.Json.JsonSerializer.GetMatchingConverter(IList`1 converters, Type objectType)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.InitializeContract(JsonContract contract)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateObjectContract(Type objectType)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract(Type objectType)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract(Type type)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
   at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
   at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer)
   at Newtonsoft.Json.JsonConvert.SerializeObject(Object value)

..but why does it take that route?

Update

A simple test case also fails:

[Fact]
public void should_be_Able_to_Serialize_settings()
{
    JsonConvert.SerializeObject(new ProjectInfo {ConnectionStringName = "Arne"});
}

Update 2

This project has worked before. It also works on a colleague's computer. The only difference I can see is that I've upgraded to VStudio 2015 Update 1. (or that I've made a silly mistake somewhere). But I've also done a hard reset to the latest revision that my colleague uses.

Why does it try to reference v5.0.5 of System.Linq.Xml? Isn't v4.0.0 the standard one for .NET 4.5? Which version of .NET does v5.0.5 belong to?

(I've never had a similar problem with Json.NET before. Is it something with VStudio 2015 / .NET 4.5.2 / VSIX project?)

Update3

Here are the dependencies. They show that Json.NET tries to reference that exact version:

enter image description here

Update:

Json.NET reference in the project file:

<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=c70b2336aed9f731, processorArchitecture=MSIL">
  <HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
  <Private>True</Private>
</Reference>

Edit 4:

My problem is that the extension fails to work as it tries to load an assembly that does not exist. From my understanding, v5.0.5 is a silverlight assembly. And I do not use silverlight.

I've tried to add an assembly redirect, but it doesn't seem to work.

<dependentAssembly>
  <assemblyIdentity name="System.Xml.Linq" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
  <bindingRedirect oldVersion="0.0.0.0-5.0.5.0" newVersion="4.0.0.0"/>
</dependentAssembly>
MarquisDeMizzle
  • 516
  • 9
  • 24
jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • Can you show a short but complete program that demonstrates the problem? What's in `settings`? – Jon Skeet Dec 01 '15 at 20:56
  • newtonsoft.json v7.0.1 via nuget – jgauffin Dec 01 '15 at 21:13
  • I got VStudio 2015 update 1 – jgauffin Dec 01 '15 at 21:14
  • I can't reproduce this with a simple console app. Are you able to reproduce this in a regular project? – Jon Skeet Dec 01 '15 at 21:21
  • I can't reproduce it either. Tried console app and unit test in class library. Will try in vsix. – Matt Johnson-Pint Dec 01 '15 at 21:22
  • Do you have the `System.Xml.Linq` assembly binding configured in your app.config/web.config? It could be you have an invalid binding configuration? –  Dec 01 '15 at 21:26
  • Json.NET has built-in converters for converting between JSON and XML, see [Converting between JSON and XML](http://www.newtonsoft.com/json/help/html/ConvertingJSONandXML.htm). [`XmlNodeConverter`](https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs) is the class that does the conversion. In your traceback, it is being called to check if the object being serialized is a [Linq to XML](https://msdn.microsoft.com/en-us/library/bb387098.aspx) type. No idea why the library fails to load though; do you have it installed? – dbc Dec 01 '15 at 21:27
  • Is the `ProjectInfo` class *exactly* as shown here - no other properties or fields, no attributes, etc. Or is there something subtle you may have not shown here? – Matt Johnson-Pint Dec 01 '15 at 21:36
  • Also, have you set anything via `JsonConvert.DefaultSettings` or just the defaults? – Matt Johnson-Pint Dec 01 '15 at 21:36
  • I can't reproduce it with a simple example either. that's the problem. I need to figure out what the dependency comes from so that I don't have to recreate the entire solution. – jgauffin Dec 01 '15 at 21:36
  • @MattJohnson: It's the exact class (only namespace differs). I had a IEnumerable it it before by removed it so that I've not included a linq expression by mistake. But the error is still there without that list. – jgauffin Dec 01 '15 at 21:38
  • @MattJohnson: I havent changed any default settings. – jgauffin Dec 01 '15 at 21:38
  • 2
    I suspect console applications are loading the dependency from the GAC, while the vsix project doesn't do that. Newtonsoft does have a dependency on it. http://i.imgur.com/xn6MVDx.png –  Dec 01 '15 at 21:43
  • @Amy: I got no binding redirect. – jgauffin Dec 01 '15 at 21:48
  • Does version 5.0.5.0 not indicate a silverlight assembly? Its a long time since I developed silverlight applications – Jehof Dec 01 '15 at 21:48
  • Can you check what is the framework selected in project properties? Is it .Net Framework Client Profile or normal framework – Baskar Rao Dec 01 '15 at 21:59
  • 1
    Well, I can tell you that `XmlNodeConverter` is one of the built-in converters that is tested for on every object deserialized with the default contract resolver. You can see it in the `BuiltInConverters` array [here](https://github.com/JamesNK/Newtonsoft.Json/blob/7.0.1/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs#L116). That doesn't run for `PORTABLE40`, but other than that it's always going to be hit. Therefore you must be able to reference its dependencies. (That array is passed as the first parameter in the call to `GetMatchingConverter`, seen in your stack trace.) – Matt Johnson-Pint Dec 01 '15 at 22:05
  • 7
    What's the problem with adding a reference to `System.Xml.Linq` in your vsix project? – Matt Johnson-Pint Dec 01 '15 at 22:06
  • Are you possibly referencing an additional assembly that is a PCL? See [this similar issue I found](https://github.com/fsprojects/Paket/issues/703) – Matt Johnson-Pint Dec 01 '15 at 22:23
  • 2
    Can you share your project file (just the file) after you added the project reference? Something tells me you picked up the wrong asset from the NuGet package... – Jason Malinowski Dec 01 '15 at 23:25
  • @MattJohnson: The problem is that Visual Studio do not find v5.0.5 and therefore crashes with a TypeLoadException. – jgauffin Dec 02 '15 at 06:54
  • @JasonMalinowski: added the project reference to JSON.NET – jgauffin Dec 02 '15 at 09:30
  • @jgauffin: thanks...unfortunately that's what it should be. :-( – Jason Malinowski Dec 02 '15 at 17:17
  • Can you remove the nuget reference, remove the file leftovers and re-add via nuget? maybe 6.08 or 8.01-beta? – Tracker1 Dec 15 '15 at 07:21

1 Answers1

2

Json.NET uses System.Xml.Linq for converting json to xml.

You're going to be able to compile without a library's dependencies, as long as you don't reference any of the types in the dependencies. This is normal. I've had the same issue with NHibernate's Iesi.Collections dependency.

I looked in the Json.Net source code and the using statements for System.Xml.Linq are conditionalized for the .Net version. Are you and your colleague running the same .Net version? Did you recently change .Net on your machine?

What I would suggest is to completely remove NHibernate and any dependencies. Then install Json.Net with NuGet. NuGet will automatically add all dependencies and perform any needed assembly binding redirects.

Even if you don't want to use NuGet, run a diff and see what changes it makes so you can make the same.

Timothy Gonzalez
  • 1,802
  • 21
  • 18