.netexceptionjson.netassembly-referencesfileloadexception

Why could this JSON.Net FileLoadException be conditional


I get this runtime exception from

var names = MeasureName.Empty.GetAll();
if (null == names)
    Console.WriteLine("No Measure Names found.");
else
{
    Console.WriteLine(JsonConvert.SerializeObject(names));
}

but not this

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

even though neither result is null. This result is repeatable. When the bottom code runs there's no exception, followed by the top section; exception.

I assume the issue is with MeasureName, but not MeasureGroup, both are classes in a reference project. I don't understand why I would get this exception though. Clearly the JSON.Net dll can be loaded, as it works for MeasureGroup, so why the "claim" that the File can't be loaded?


Update, full Exception:

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: 

Solution

  • I got the dll versions to match all across and it works now.

    I thought I had the same version between my class library project and console app project because I had uninstalled and reinstalled using the package manager (which was ok) then selected the latest version of the dll for the console project using the package manager, just searching Assemblies for json and selecting my framework. That was my downfall. I don't know why 6.0 was the latest offered, see below: Reference Manager screenshot showing latest dll to add If I just browsed my project/package directory I could just add the 9.0.1 that I was using there, then the serialization in the console project worked. Thanks for the link @dbc