0

我是 wcf 的新手,我创建了一个这样的 DataContract:

    [DataContract]
    public class directoriesUser
    {
        [DataMember]
        public string NameDir { get; set; }

        [DataMember]
        public directoriesUser Parent { get; set; }


        private ObservableCollection<directoriesUser> _userDirs = new ObservableCollection<directoriesUser>();

        [DataMember]
        public ObservableCollection<directoriesUser> UserDirectories
        {
            get { return _userDirs; }
            set { _userDirs = value; }
        }
    }

但是当我像上面一样使用这个类时,服务会抛出异常。有人能帮我吗 ?

    public directoriesUser getHomeUser(int user_id)
    {
        directoriesUser dirhome = new directoriesUser();
        // this doesn't works
        dirhome.UserDirectories = new ObservableCollection<directoriesUser>{
            new directoriesUser { NameDir = "dir1", Parent = dirhome},
            new directoriesUser {NameDir = "dir2", Parent = dirhome}
        };
        // neither this one
        dirhome.UserDirectories.Add(new directoriesUser {NameDir = "dir3", Parent = dirhome})
        return dirhome;
    }
4

1 回答 1

0

当 wcf 序列化实例时,您的目录用户类可能会导致递归。这基本上是由指向层次结构的 Parent 属性引起的。这是关于此的更详细的文章

于 2011-06-08T17:36:49.563 回答