Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 375
wpfwannabe Avatar asked Aug 15 '11 15:08

wpfwannabe


3 Answers

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

like image 55
wpfwannabe Avatar answered Oct 23 '22 10:10

wpfwannabe


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;
    }
like image 4
VIRA Avatar answered Oct 23 '22 09:10

VIRA


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.

like image 7
Roland Avatar answered Oct 23 '22 10:10

Roland