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:
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>
settings
? – AerophoneSystem.Xml.Linq
assembly binding configured in your app.config/web.config? It could be you have an invalid binding configuration? – DespicableXmlNodeConverter
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 type. No idea why the library fails to load though; do you have it installed? – HappenProjectInfo
class exactly as shown here - no other properties or fields, no attributes, etc. Or is there something subtle you may have not shown here? – RichersonJsonConvert.DefaultSettings
or just the defaults? – RichersonXmlNodeConverter
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 theBuiltInConverters
array here. That doesn't run forPORTABLE40
, 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 toGetMatchingConverter
, seen in your stack trace.) – RichersonSystem.Xml.Linq
in your vsix project? – Richerson