Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

c# - Disable Type Hinting in WCF JSON Services

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

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

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm assuming you are using <enableWebScript/> in your behavior config, replace that with <webHttp defaultOutgoingResponseFormat="Json"/> and you will get nice and clean json


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...