我有一个简单的WCF服务,它有一个返回“复杂”对象的方法(由简单和集合属性组成)。当我调用此方法并查看生成的XML时,集合中的项目缺少值;但是,如果我在调试器中检查.net对象,则会正确填充集合及其值。
包含集合的类:
[DataContract]
public class MetadataResponse
{
[DataMember]
public int index;
[DataMember]
public int count;
[DataMember]
public int total;
[DataMember]
public MediaCollection[] mediaCollection;
[DataMember]
public MediaMetadata[] mediaMetadata;
}
包含的项目类:
[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;
}
返回对象的方法:
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;
}
检查填充对象的调试器结果:
{MurfieSoapApi.MetadataResponse}
count: 3
index: 0
mediaCollection: {MurfieSoapApi.MediaCollection[3]}
mediaMetadata: null
total: 3
方法调用的XML结果(仍试图弄清楚如何让StackOverflow格式化这个...):
<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>
我尝试过其他集合类型而不是数组(List等),但结果总是一样的。我正在使用默认的web.config值,这些值对于返回简单结果的其他方法效果很好,但是当涉及到这种返回类型时,自动映射器可能无法完成任务......不确定。
答案 0 :(得分:1)
因为你错过了
来自MediaCollection类的数据成员
属性。
而不是:
[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;
}
你应该这样写:
[DataContract]
public class MediaCollection
{
[DataMember]
public String id;
[DataMember]
public String title;
....
}
答案 1 :(得分:0)
MediaCollection字段未被序列化。您需要将[DataMember]属性添加到字段中。
[DataContract]
public class MediaCollection
{
[DataMember]
public String id;
[DataMember]
public String title;
[DataMember]
public Enum itemType;
[DataMember]
public String artistId;
...etc...
}