RicoSuter / NJsonSchema

JSON Schema reader, generator and validator for .NET
http://NJsonSchema.org
MIT License
1.37k stars 529 forks source link

System.TypeLoadException: Could not load type 'NJsonSchema.Annotations.JsonSchemaFlattenAttribute' from assembly 'NJsonSchema, Version=11.0.0.0 #1677

Closed SeppPenner closed 1 month ago

SeppPenner commented 6 months ago

I get the exception after I upgraded my project to Net8 and added all the Net8 related packets.

An error occurred: System.TypeLoadException: Could not load type 'NJsonSchema.Annotations.JsonSchemaFlattenAttribute' from assembly 'NJsonSchema, Version=11.0.0.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102'.
   at System.ModuleHandle.ResolveType(QCallModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
   at System.ModuleHandle.ResolveTypeHandle(Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
   at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
   at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(MetadataToken caCtorToken, MetadataImport& scope, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1& derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctorWithParameters, Boolean& isVarArg)
   at System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder`1& attributes, RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1 derivedAttributes)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
   at Newtonsoft.Json.Utilities.ReflectionUtils.GetAttributes(Object attributeProvider, Type attributeType, Boolean inherit)
   at Newtonsoft.Json.Serialization.JsonTypeReflector.GetAssociateMetadataTypeFromAttribute(Type type)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Newtonsoft.Json.Utilities.ThreadSafeStore`2.Get(TKey key)
   at Newtonsoft.Json.Serialization.JsonTypeReflector.GetAssociatedMetadataType(Type type)
   at Newtonsoft.Json.Serialization.JsonTypeReflector.GetAttribute[T](Type type)
   at Newtonsoft.Json.Serialization.JsonTypeReflector.GetAttribute[T](Object provider)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Newtonsoft.Json.Utilities.ThreadSafeStore`2.Get(TKey key)
   at Newtonsoft.Json.Serialization.CachedAttributeGetter`1.GetAttribute(Object type)
   at Newtonsoft.Json.Serialization.JsonTypeReflector.GetCachedAttribute[T](Object attributeProvider)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract(Type objectType)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Newtonsoft.Json.Utilities.ThreadSafeStore`2.Get(TKey key)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract(Type type)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.GetContract(Type type)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.GetContractSafe(Type type)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)

NSwag.AspNetCore is on 14.0.3, NJsonSchema is on 11.0.0 for all (transient) dependent packets. Newtonsoft.Json is on 13.0.3 for all (transient) dependent packets.

markovujic89 commented 1 month ago

Is there any fix for this problem? I faced with similar exception, after migration my app from .NET 6 to .NET 8:

"Type": "System.TypeLoadException", "Message": "Could not load type 'NJsonSchema.Annotations.JsonSchemaTypeAttribute' from assembly 'NJsonSchema, Version=11.0.2.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102'.", "Source": "System.Private.CoreLib",

lahma commented 1 month ago

You are referencing old binaries somewhere which do not know that JsonSchemaTypeAttribute should be loaded from assembly NJsonSchema.Annotations.

markovujic89 commented 1 month ago

@lahma Yes, that was an issue. I had an old version of NJsonSchema in my app, which contains JsonSchemaTypeAttribute. After upgrading to the latest version of NSwag.AspNetCore (NJsonSchema), I should read JsonSchemaTypeAttribute from NJsonSchema.Annotations. So I fix it in the meantime on that way.