简体   繁体   English

为什么Json.NET需要System.Xml.Linq v5.0.5来序列化一个简单的对象?

[英]Why does Json.NET require System.Xml.Linq v5.0.5 for serialization of a simple object?

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): 我尝试做一个简单的序列化(在VSIX项目中):

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. 我花了最后一小时试图找出依赖关系的来源或Json.NET为什么尝试使用该命名空间。 System.Xml.Linq is not referenced in any of my projects. 我的任何项目都没有引用System.Xml.Linq。

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 更新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 . 我能看到的唯一区别是我已升级到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? 为什么它试图引用System.Linq.Xml的v5.0.5? Isn't v4.0.0 the standard one for .NET 4.5? v4.0.0不是.NET 4.5的标准版吗? Which version of .NET does v5.0.5 belong to? v5.0.5属于哪个版本的.NET?

(I've never had a similar problem with Json.NET before. Is it something with VStudio 2015 / .NET 4.5.2 / VSIX project?) (我之前从未遇到类似Json.NET的问题。它是否与VStudio 2015 / .NET 4.5.2 / VSIX项目有关?)

Update3 UPDATE3

Here are the dependencies. 这是依赖项。 They show that Json.NET tries to reference that exact version: 他们表明Json.NET试图引用那个确切的版本:

在此输入图像描述

Update: 更新:

Json.NET reference in the project file: 项目文件中的Json.NET参考:

<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: 编辑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. 据我了解,v5.0.5是一个silverlight程序集。 And I do not use silverlight. 我不使用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>

Json.NET uses System.Xml.Linq for converting json to xml. Json.NET使用System.Xml.Linq将json转换为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. 我对NHibernate的Iesi.Collections依赖有同样的问题。

I looked in the Json.Net source code and the using statements for System.Xml.Linq are conditionalized for the .Net version. 我查看了Json.Net源代码,System.Xml.Linq的using语句是以.Net版本为条件的。 Are you and your colleague running the same .Net version? 你和你的同事是否运行相同的.Net版本? Did you recently change .Net on your machine? 你最近在你的机器上改变了.Net吗?

What I would suggest is to completely remove NHibernate and any dependencies. 我建议完全删除NHibernate和任何依赖项。 Then install Json.Net with NuGet. 然后使用NuGet安装Json.Net。 NuGet will automatically add all dependencies and perform any needed assembly binding redirects. NuGet将自动添加所有依赖项并执行任何所需的程序集绑定重定向。

Even if you don't want to use NuGet, run a diff and see what changes it makes so you can make the same. 即使您不想使用NuGet,也可以运行差异并查看它所做的更改,以便您可以进行相同的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM