Can I say that the WCF WebAPI serializer ignores nested class objects?

I am using WCF WebAPI to write REST services using WCF. I am returning my POCO classes as json / xml objects from my service. Most of my POCO classes contain ICollection , as they are part of EF4.1 Code First, so I get the error:

It is not possible to serialize a member of type ... System.Collections.Generic.ICollection ... because it has a type - because it is an interface

To avoid < XMLIgnore and ScriptIgnore . And there are some problems in custom JSON serialization in WCF .

I just thought that someone might run into a similar problem and have a better solution or way of setting serialization classes, otherwise I will have to decorate each such attribute with XMLIgnore , etc.

+4
source share
4 answers

I found that in WCF WebAPI [ScriptIgnore] or [JsonIgnore] does not work and does not work, so I am returning to ASP.NET MVC for json-related REST APIs.

Update [24-Jul-2012] This answer is old, and Microsfot WebAPI has come a long way, please before relying on this answer.

0
source

Most often, you are likely to use DTO or (browse models in ASP.NET MVC) to separate from your domain model. And you can use AutoMapper to minimize code conversion between domain models and DTO.

+3
source

You must add [XmlIgnore] and [IgnoreDataMember]. Then the property will be ignored for xml and json response.

+2
source

Correctly need Sandro, [IgnoreDataMember] and [XmlIgnore] .

A little more explanation: [IgnoreDataMember] needed to exclude a field in json serialization, [XmlIgnore] needed to exclude a field in xml serialization.

+1
source

All Articles