0

Since I pull my project from GitHub and attempt to run my app the problem keep happening when it refers to Json.net when trying to serialize anything. Originally it was at version 11 when the error showed up, then I tried to downgrade to version 8 and it still persists. So, I upgrade to latest on 12.0.1 but the problem still not going away

The code is nothing but Newtonsoft.Json.JsonConvert.SerializeObject(data)

And no matter what type of data it is it always thrown

FieldAccessException: Attempt by method 
'Newtonsoft.Json.JsonSerializerSettings..cctor()' to access field 
'Newtonsoft.Json.JsonSerializerSettings.DefaultContext' failed.

I even tried to construct the JsonSerializerSettings on my own and that still happens.

ToonWK
  • 47
  • 1
  • 8
  • Could you create a simple app reproducing this issue and post its source code somewhere like GitHub? I would try it out locally – Martin Zikmund Feb 12 '19 at 09:13

2 Answers2

0

Will this answer help you?

Basically, in order to workaround this problem you need to make sure "Enable the Visual Studio hosting process" is unchecked in your project's settings under Debug.

wata
  • 76
  • 4
0

This error may occur if the code is running under partial trust. Following link can help more in providing error description:

https://learn.microsoft.com/en-us/dotnet/api/system.methodaccessexception?redirectedfrom=MSDN&view=netframework-4.7.2

Previously, I have encountered this error if the code has restricted access like private, protected or internal methods. As per MSDN:

This exception is thrown in situations such as the following:

  • A private, protected, or internal method that would not be accessible from normal compiled code is accessed from partially trusted code by using reflection.

  • A security-critical method is accessed from transparent code.

  • The access level of a method in a class library has changed, and one or more assemblies that reference the library have not been
    recompiled.

Gauravsa
  • 6,330
  • 2
  • 21
  • 30