I have a simple WCF service which has a method that returns a “complex” object (made up of simple and collection properties). When I call this method and look at the resulting XML, the values are missing for the items in the collection; however, if I examine the .net object in the debugger, the collection and its’ values are populated correctly.
The class containing the collection:
[DataContract]
public class MetadataResponse
{
[DataMember]
public int index;
[DataMember]
public int count;
[DataMember]
public int total;
[DataMember]
public MediaCollection[] mediaCollection;
[DataMember]
public MediaMetadata[] mediaMetadata;
}
The contained item class:
[DataContract]
public class MediaCollection
{
public String id;
public String title;
public Enum itemType;
public String artistId;
public String artist;
public String albumArtURI;
public Boolean canPlay;
public Boolean canEnumerate;
public Boolean canAddToFavorites;
public Boolean canScroll;
public Boolean canSkip;
}
The method that returns the object:
public MetadataResponse getMetadata(string id, int index, int count, bool recursive)
{
MetadataResponse metadataResponse = new MetadataResponse();
MediaCollection mediaCollectionItem = new MediaCollection();
// return a static collection for now
metadataResponse.index = 0;
metadataResponse.count = 3;
metadataResponse.total = 3;
metadataResponse.mediaCollection = new MediaCollection[3];
// add some items
mediaCollectionItem.id = "0001";
mediaCollectionItem.title = "foo";
metadataResponse.mediaCollection[0] = mediaCollectionItem;
mediaCollectionItem.id = "0002";
mediaCollectionItem.title = "bar";
metadataResponse.mediaCollection[1] = mediaCollectionItem;
mediaCollectionItem.id = "0003";
mediaCollectionItem.title = "choo";
metadataResponse.mediaCollection[2] = mediaCollectionItem;
return metadataResponse;
}
Debugger result examining the populated object:
{MurfieSoapApi.MetadataResponse}
count: 3
index: 0
mediaCollection: {MurfieSoapApi.MediaCollection[3]}
mediaMetadata: null
total: 3
The XML result from the method call (still trying to figure out how to get StackOverflow to format this one…):
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header />
<s:Body>
<getMetadataResponse xmlns="http://www.sonos.com/Services/1.1">
<getMetadataResult xmlns:a="http://schemas.datacontract.org/2004/07/MurfieSoapApi" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:count>3</a:count>
<a:index>0</a:index>
<a:mediaCollection>
<a:MediaCollection />
<a:MediaCollection />
<a:MediaCollection />
</a:mediaCollection>
<a:mediaMetadata i:nil="true" />
<a:total>3</a:total>
</getMetadataResult>
</getMetadataResponse>
</s:Body>
</s:Envelope>
I have tried other collection types instead of arrays (List, etc.) but the results were always the same. I’m using the default web.config values which has worked well for other methods which return simple results, but maybe the automatic mapper isn’t up to the task when it comes to this return type…not sure.
Because you are missing the
attributes from the MediaCollection class.
Instead of this:
You should write this: