Do WCF and DataContractSerializer serialize CollectionDataContract-decorated collection types in different ways?

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

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

I am wondering if there is any decoration that I am missing in the service contract.

More details:

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

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

If I just create an instance of Foo and then use DataContractSerializer.WriteObject to serialize it, I get what you expect:

<FooList>
    <Foo>
        <BarString>myString1</BarString>
    </Foo>
</FooList>

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

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

GET http://www.someEndpoint.com/foos/, :

<ArrayOfFoo>
    <Foo>
        <BarString>myString1</BarString>
    </Foo>
</ArrayOfFoo>

Name= "MyFooListName" CollectionDataContract. : DataContractSerializer ; WCF .

+5
3

: XmlSerializer, DataContractSerializer.

XmlSerializer... ... .

, XmlSerializerFormat :

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

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

Foo XML-. , , XmlSerializer, DataContractSerializer.

XmlSerializerFormat, : WCF FooList , .

+3

. MSDN:

DataContractSerializer , XmlSerializer ASP.NET Web . , , XmlElementAttribute XmlAttributeAttribute. , WCF XmlSerializer DataContractSerializer.

, XMLSerializer, .

+2

Have you chosen common types when configuring the WCF service? if not then

right-click and go to setup, then select the Generic Type, the default is the arraylist type.

0
source

All Articles