尝试通过方法“System.Web.Helpers.Json..cctor()"访问方法“System.Web.Helpers.Json.CreateSerializer()"失败 [英] Attempt by method 'System.Web.Helpers.Json..cctor()' to access method 'System.Web.Helpers.Json.CreateSerializer()' failed

查看:29
本文介绍了尝试通过方法“System.Web.Helpers.Json..cctor()"访问方法“System.Web.Helpers.Json.CreateSerializer()"失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 System.Web.Helpers.Json 在 NET 4 中将一些 JSON 反序列化为 dynamic.以下行失败并出现此错误:TypeInitializationException:尝试通过方法System.Web.Helpers.Json..cctor()"访问方法System.Web.Helpers.Json.CreateSerializer()"失败.

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);

response 很长但有效的 JSON.这里可能有什么问题?我已经尝试了 LINQPad 和一个简短的手工 JSON 并且它有效.这是某种配置问题吗?

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?

这是实际的示例 JSON.看起来内容几乎无关紧要.当它在全新的控制台应用程序或 LINQPad 中运行时,它会按预期工作.但是,如果您尝试从全新的 Windows 窗体应用程序运行相同的代码,则会出现上述错误.

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"}}}");

实际上,这与项目类型无关.如果正在调试项目,则会引发异常.如果只是运行,则不会发生异常.很奇怪吧?

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?

推荐答案

我忘记了这个问题,同时我找到了我的答案.我认为它在 Microsoft 的 Connect 站点上的某个地方,但我不确定.所以现在让我们分享一下.

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.

基本上,为了解决此问题,您需要确保在调试"下的项目设置中取消选中启用 Visual Studio 托管进程".我不确定为什么会发生这种情况,但这绝对是一种修复"它的方法.一旦我发现了这一点,我就停止了寻找答案.对我来说已经足够了.

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.

这篇关于尝试通过方法“System.Web.Helpers.Json..cctor()"访问方法“System.Web.Helpers.Json.CreateSerializer()"失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆