在WCF JSON服务禁用类型提示 [英] Disable Type Hinting in WCF JSON Services

查看:105
本文介绍了在WCF JSON服务禁用类型提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有什么应该是一个比较简单的问题,我似乎无法找到答案。

I have what should be a relatively simple question that I can't seem to find an answer for.

在WCF执行对象的序列化,它会自动应用类型提示。对JSON的服务,这将导致一个额外的字段名为每个复杂的对象 __类型。定义为一个对象:

When WCF performs its serialization of objects, it automatically applies Type Hinting. For Json services, this results in an extra field on each complex object called __type. An object defined as:

[DataContract]
public class SomeObject
{
    [DataMember]
    public string First { get; set; }

    [DataMember]
    public string Last { get; set; }
}

将序列来是这样的:

Would serialize to something like:

{
    "First" : "Hello",
    "Last" : "World!",
    "__type" : "SomeObject#MyNamespace.SomeObject"
}

通常,这不是一个问题。不幸的是,当你开始嵌套类到相当大,结构复杂,这导致一吨的开销在JSON响应将返回给客户端的大小。

Normally this isn't an issue. Unfortunately when you start nesting classes into fairly large and complex structures, this results in a ton of overhead in the size of the JSON response going back to the client.

当然,必须有一种方法来禁用此行为,但我一直没能找到一个(没有​​过的里克施特拉尔早在2007年 ......但它的2010,我希望有人已经想通了这一点)。

Surely there has to be a way to disable this behavior but I haven't been able to find one (neither had Rick Strahl back in 2007...but it's 2010 and I hope somebody has figured this out).

推荐答案

使用 DataContractJsonSerializer 会想这样做是为了支持多态,并能够反序列化回一个公知的类型。 NewtonSoft是第三方JSON序列化,不会增加__type提示。如果你只是序列化,而不是使用一个真正的进步DataContract属性,你可能想尝试一下。

Using the DataContractJsonSerializer is going to want to do this to support polymorphism and be able to deserialize back to a known type. NewtonSoft is a third party json serializer that won't add the __type hint. If your just serializing and not using an real advance DataContract attributes, you may want to give it a try.

这篇关于在WCF JSON服务禁用类型提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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