为什么这个 JSON.Net FileLoadException 可以是有条件的

Why could this JSON.Net FileLoadException be conditional

我从

得到这个运行时异常
var names = MeasureName.Empty.GetAll();
if (null == names)
    Console.WriteLine("No Measure Names found.");
else
{
    Console.WriteLine(JsonConvert.SerializeObject(names));
}

但不是这个

var groups = MeasureGroup.Empty.GetAll();
if (null == groups)
    Console.WriteLine("No Measure Groups found.");
else
{
    Console.WriteLine(JsonConvert.SerializeObject(groups));
}

即使两个结果都不为空。这个结果是可重复的。当底部代码运行时没有异常,然后是顶部代码;异常。

我假设问题出在 MeasureName,而不是 MeasureGroup,两者都在参考项目中 类。我不明白为什么我会得到 this 异常。明明可以加载 JSON.Net dll,因为它适用于 MeasureGroup,那么为什么 "claim" 无法加载文件?


更新,完整异常:

System.IO.FileLoadException was unhandled
  HResult=-2146234304
  Message=Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
  Source=mscorlib
  FileName=Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
  FusionLog==== Pre-bind state information ===
LOG: DisplayName = Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
 (Fully-specified)
LOG: Appbase = file:///.../bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : DataLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: ..\bin\Debug\UpdateData.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
LOG: Attempting download of new URL file:///C:/.../bin/Debug/Newtonsoft.Json.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

  StackTrace:
       at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
       at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
       at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
       at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
       at System.Reflection.CustomAttribute.IsCustomAttributeDefined(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Int32 attributeCtorToken, Boolean mustBeInheritable)
       at System.Reflection.CustomAttribute.IsDefined(RuntimePropertyInfo property, RuntimeType caType)
       at System.Reflection.RuntimePropertyInfo.IsDefined(Type attributeType, Boolean inherit)
       at Newtonsoft.Json.Serialization.DefaultContractResolver.GetSerializableMembers(Type objectType)
       at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateProperties(Type type, MemberSerialization memberSerialization)
       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.WriteStartArray(JsonWriter writer, Object values, JsonArrayContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value)
       at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value)
       at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)
       at Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Formatting formatting, JsonSerializerSettings settings)
       at Newtonsoft.Json.JsonConvert.SerializeObject(Object value)
       at UpdateData.Program.Queries() in ..\Program.cs:line 81
       at UpdateData.Program.Main(String[] args) in ..\Program.cs:line 27
  InnerException: 

我得到了所有版本都匹配的 dll 版本,现在可以使用了。

我认为我的 class 库项目和控制台应用程序项目之间有相同的版本,因为我已经卸载并使用包管理器重新安装(没问题)然后选择了最新版本的 dll使用包管理器的控制台项目,只需在程序集中搜索 json 并选择我的框架。那是我的失败。我不知道为什么 6.0 是最新提供的,见下文: 如果我只是浏览我的 project/package 目录,我可以只添加我在那里使用的 9.0.1,然后控制台项目中的序列化工作。 感谢 link @dbc