Do WCF and DataContractSerializer serialize CollectionDataContract-decorated collection types differently?

I have a really simple customized collection type that inherits from List<> and uses a CollectionDataContract.

When I use DataContractSerializer.WriteObject to serialize it, it respects the CollectionDataContract attribute the way I'd expect; however, when I use it as a return type for a WCF method, I get the default ArrayOfFoo.

I'm wondering if there is some decoration I'm missing in the service contract.


[DataContract(Namespace = "")]
public class Foo
    public string BarString { get; set; }

[CollectionDataContract(Namespace = "")]
public class FooList : List<Foo> {}

If I just instantiate a Foo and then use DataContractSerializer.WriteObject to serialize it, I get what you'd expect:


However, if I have a service with a method like this...

[ServiceContract Name = "MyService"]
public interface IMyService
    [OperationContract, WebGet(UriTemplate = "foos/")]
    FooList GetAllFoos();

and then do a GET for, I get this:


I've also tried specifying Name="MyFooListName" in the CollectionDataContract attribute. Same results: DataContractSerializer gets the memo; WCF doesn't.


Saeed sent me in the right direction: I inadvertently ended up with XmlSerializer, when I had been hoping for DataContractSerializer.

I had ended up with XmlSerializer... well... by asking for it.

In particular, I had decorated methods in my service with the XmlSerializerFormat like this:

[ServiceContract Name = "MyService"] 
public interface IMyService 
    //  ... other stuff ...

    [OperationContract, WebInvoke(UriTemplate = "foos/", Method = "POST")] 
    Foo PostAFoo(Foo yourNewFoo);

I had done this in the hopes of forgiving member order in hand-rolled Foo XML blobs. Of course, when one does this one ends up with XmlSerializer, not DataContractSerializer.

When I take away the XmlSerializerFormat attribute, problem solved: WCF is now serializing my FooList collection the way I want.

Need Your Help

getLastAccessedTime() in GWT sessions returns 0

google-app-engine session gwt

I'm trying to add timeouts to GWT sessions, by using the following code to check if a session is alive:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.