0

我有一个简单的 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>

我尝试过其他集合类型而不是数组(列表等),但结果始终相同。我正在使用默认的 web.config 值,该值对于返回简单结果的其他方法效果很好,但是当涉及到这种返回类型时,自动映射器可能无法胜任任务......不确定。

4

2 回答 2

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;

     ....
    }
于 2012-04-11T14:14:09.530 回答
0

MediaCollection 字段没有被序列化。您需要将 [DataMember] 属性添加到您的字段。

[DataContract]
    public class MediaCollection
    {
        [DataMember]
        public String id;
        [DataMember]
        public String title;
        [DataMember]
        public Enum itemType;
        [DataMember]
        public String artistId;
        ...etc...
    }
于 2012-04-11T14:15:31.500 回答