0

我从这个运行时异常

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,两者都是参考项目中的类。我不明白为什么我会得到这个例外。显然 JSON.Net dll 可以加载,因为它适用于 MeasureGroup,那么为什么“声称”文件无法加载?


更新,完整例外:

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: 
4

1 回答 1

0

我得到了完全匹配的 dll 版本,它现在可以工作了。

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

于 2017-06-08T13:45:57.227 回答