简体   繁体   English

无法在本地重现NewtonSoft反序列化错误

[英]Unable to reproduce NewtonSoft deserialization error locally

I'm getting following error in deserializing a json object : (Using JSON.NET) 我在反序列化json对象时遇到以下错误:(使用JSON.NET)

Cannot create an abstract class. 无法创建抽象类。
at System.Runtime.Serialization.FormatterServices.nativeGetUninitializedObject(RuntimeType type) 在System.Runtime.Serialization.FormatterServices.nativeGetUninitializedObject(RuntimeType类型)
at ReadDataSourceBaseFromJson(XmlReaderDelegator , XmlObjectSerializerReadContextComplexJson , XmlDictionaryString , XmlDictionaryString[] ) 在ReadDataSourceBaseFromJson(XmlReaderDelegator,XmlObjectSerializerReadContextComplexJson,XmlDictionaryString,XmlDictionaryString [])中

And exact line giving the error is: 给出错误的确切行是:

return _serializer.ReadObject(stream); // _serializer is DataContractJsonSerializer

I can not reproduce this issue in my local machine, or in the stage machine in hosted environment. 我无法在本地计算机或托管环境中的舞台计算机中重现此问题。 JSON is deserialized perfectly JSON完美反序列化

However the issue is reproducible consistently in the QA and production environment. 但是,此问题在质量检查和生产环境中始终可以重现。 I've tried with 我尝试过

  • QA database 质量检查数据库
  • QA code in release mode, including all DLLs 发布模式下的质量检查代码,包括所有DLL
  • QA web configs 质量检查网页配置

But still can't produce. 但是仍然无法生产。 JSON object is stored in database and I'm trying to serialize it into classes. JSON对象存储在数据库中,我正在尝试将其序列化为类。

What else I can do to reproduce the issue? 我还可以做些什么来重现该问题?

EDIT 编辑

Code leading up to the error line is: 导致错误行的代码是:

var json = JObject.Parse(definition);
var foo = json.GetValue("outerRoot").ToString();
var dataDefinition = _serializer.Deserialize(foo);

And Deserialize function is 反序列化功能是

public returnType Deserialize(string serializedData)
        {
            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(serializedData)))
            {
                return (returnType)_serializer.ReadObject(stream);
            }
        }

So I am using JSON.Net to strip out the outer most node and deserializing what's inside. 所以我正在使用JSON.Net去除最外面的节点并反序列化内部内容。

When I encounter something that only happens on PROD I will set the application to run on Local IIS instead of IIS Express. 当我遇到仅在PROD上发生的事情时,我会将应用程序设置为在本地IIS而不是IIS Express上运行。 This requires that you have IIS installed on your development machine, however this allows you to debug locally and hit the site from a remote computer to cause the error. 这要求您在开发计算机上安装了IIS,但是,这允许您在本地调试并从远程计算机访问该站点以引起错误。

In your code where you instantiate the DataContractJsonSerializer , you will be specifying the deserializing type. 在实例化DataContractJsonSerializer代码中,将指定反序列化类型。 The code will look something like this: 该代码将如下所示:

_serializer = new DataContractJsonSerializer(typeof(MyType));

It appears that the type that you have specified for deserialization has the abstract keyword. 您为反序列化指定的类型似乎具有abstract关键字。

public abstract class MyType

In C# you can't instatiate abstract classes, only non-abstract classes, ie classes that inherit from them. 在C#中,您不能仅使非抽象类(即从其继承的类)实例化抽象类。

This was Framework 4.0 issue, and fixed in Framework 4.5 WCF JSON Deserialization Issues with Whitespace 这是Framework 4.0问题,已在带有空格的 Framework 4.5 WCF JSON反序列化问题中修复。

Solution was to upgrade Framework. 解决方案是升级Framework。 Or what dbc said in the comments 还是dbc在评论中说的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM