Attempt by method 'System.Web.Helpers.Json..cctor()' to access method 'System.Web.Helpers.Json.CreateSerializer()' failed

C#.NetJson.Net 4.0

C# Problem Overview


I am using System.Web.Helpers.Json to deserialize some JSON into dynamic in NET 4. The following line fails with this error: TypeInitializationException: Attempt by method 'System.Web.Helpers.Json..cctor()' to access method 'System.Web.Helpers.Json.CreateSerializer()' failed.

var json = Json.Decode(response);

The response is lengthy but valid JSON. What could be the matter here? I have tried LINQPad with a short handcrafted JSON and it worked. Is this a configuration issue of some sort?

[EDIT]

Here is the actual sample JSON. It appears the content is pretty much irrelevant. When this is run in a brand new Console application or LINQPad, it works as expected. But if you try to run the same code from a brand new Windows Forms application, it barfs with the above error.

var json = Json.Decode("{\"r\":{\"0\":{\"id\":\"2\"},\"1\":{\"id\":\"33\"}}}");

[EDIT2]

Actually, it turns out this has nothing to do with project types. The exception is thrown if the project is being debugged. If it is simply run, the exception does not occur. Strange, eh?

C# Solutions


Solution 1 - C#

I forgot about this question and I found my answer in the meantime. I think it was somewhere on Microsoft's Connect site but I am not sure. So let's share it now.

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. I am not sure why it's happening but this is definitely a way to "fix" it. I stopped searching for answers once I found out about this. It was good enough for me.

Settings / Debug

Solution 2 - C#

This can also happen if you are running in a partial trust. Check the exception description here for possible reasons.

I don't know if this will apply to you, since you are not running in a web context, but this is what that link describes:

> 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.

Solution 3 - C#

There is problem in the inbuilt json class.

If you want to achieve this in alternate way, please use the below code:

JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new DynamicJavaScriptConverter[] { new DynamicJavaScriptConverter() });
var result = WrapObject(serializer.DeserializeObject(value)); // here you will have result.

private object WrapObject(object value)
    {
        IDictionary<string, object> values = value as IDictionary<string, object>;
        if (values != null)
        {
            return new DynamicJsonObject(values);
        }
        object[] arrayValues = value as object[];
        if (arrayValues != null)
        {
            return new DynamicJsonArray(arrayValues);
        }
        return value;
    }

Solution 4 - C#

Further to Roland's answer: some assembly mismatches listed can be fixed in the AssemblyInfo.cs file.

The offending line in my AssemblyInfo was this:

[assembly: AllowPartiallyTrustedCallers]

Removing this allowed me to access the public property (on a public class) that I was trying to set from another assembly that had dynamically loaded this assembly.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwpfwannabeView Question on Stackoverflow
Solution 1 - C#wpfwannabeView Answer on Stackoverflow
Solution 2 - C#RolandView Answer on Stackoverflow
Solution 3 - C#VIRAView Answer on Stackoverflow
Solution 4 - C#JoelCView Answer on Stackoverflow