1

My project has recently started to throw the following error:

Unexpected error: Could not load type 'Newtonsoft.Json.Serialization.CamelCaseNamingStrategy' from assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'.
   at System.Reflection.CustomAttribute._GetPropertyOrFieldData(RuntimeModule pModule, Byte** ppBlobStart, Byte* pBlobEnd, String& name, Boolean& bIsProperty, RuntimeType& type, Object& value)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
   at Newtonsoft.Json.Utilities.ReflectionUtils.GetAttributes[T](ICustomAttributeProvider attributeProvider, Boolean inherit)
   at Newtonsoft.Json.Utilities.ReflectionUtils.GetAttribute[T](ICustomAttributeProvider attributeProvider, Boolean inherit)
   at Newtonsoft.Json.Serialization.JsonTypeReflector.GetAttribute[T](Type type)
   at Newtonsoft.Json.Utilities.ThreadSafeStore`2.AddValue(TKey key)
   at Newtonsoft.Json.Utilities.ThreadSafeStore`2.Get(TKey key)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract(Type objectType)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract(Type type)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.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 Extension.Command.ClientInitializer() in C:\project\Command.cs:line 18
in Command.Initializer

I don't really know what I might have changed to make this appear.

I'm using fody.costura to bundle all my dependencies and I've made sure it's bundled.

Here are the properties of the Newtonsoft.Json dependency:

Name                : Newtonsoft.Json
Aliases             : global
Copy Local          : True
Culture             : 
Description         : Json.NET is a popular high-performance JSON framework for .NET
Embed Interop Types : False
File Type           : Assembly
Identity            : Newtonsoft.Json
Path                : C:\project\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll
Resolved            : True
Runtime Version     : v4.0.30319
Specific Version    : True
Strong Name         : True
Version             : 12.0.0.0

My project is a .NET Framework 4.6.1 Class Library project.

Any suggestions as to what might be the problem?

Thanks!

mottosson
  • 3,283
  • 4
  • 35
  • 73
  • https://stackoverflow.com/questions/38671641/could-not-load-file-or-assembly-newtonsoft-json-version-9-0-0-0-culture-neutr please see this answer – panoskarajohn Oct 11 '19 at 15:27
  • Thanks. But it doesn't seem to be any problem loading the assembly. Just the type from the assembly. – mottosson Oct 11 '19 at 15:37
  • Does the exception have an inner exception? That class must depend on a type in an assembly that could not be loaded. Check that all of Newtonsoft's dependencies are also embedded by Fody. –  Oct 11 '19 at 17:22
  • I added the full exception to my question. – mottosson Oct 14 '19 at 07:13
  • Are you able to provide a [mcve]? That would make it a lot easier for us to help you. (It at least looks like that type is there in 12.0.2...) – Jon Skeet Oct 14 '19 at 07:31
  • It seems Fody.Costura is the culprit. When I removed it I didn't get the error message anymore. The strange thing is that it was working before... The output dll is smaller than the Newtonsoft.Json dll on it's own, which seems strange. – mottosson Oct 14 '19 at 13:32
  • Costura is being deprecated https://github.com/Fody/Costura/issues/442 so you should probably stop using it – Simon Oct 29 '19 at 02:28
  • as for the smaller dll. costura uses compression – Simon Oct 29 '19 at 02:28
  • Thanks @Simon for notifying me on this. – mottosson Oct 29 '19 at 07:55

0 Answers0