【问题标题】:sending nested hashtable发送嵌套哈希表
【发布时间】:2012-02-12 13:00:22
【问题描述】:

当我在 wcf 中创建一个嵌套的 hastable 时,我收到一个错误,即“底层连接已关闭:连接已意外关闭。” 我不知道问题出在哪里。

客户端代码

    private static List<Playlist> remotePlaylistArray;
    private static List<Playlist> _PlaylistArray;
    public static List<Playlist> PlaylistArray
    {
        get
        {
            if (_PlaylistArray == null)
            {
                _PlaylistArray = myClient.GetPlaylists(Username, Password, (int)UserInf["id"], "%").Select(i => new Playlist
                (
                    i["name"].ToString(),
                    true,
                    ((Hashtable[])i["medias"]) == null ? null :
                    ((Hashtable[])i["medias"]).Select(ix => new YoutubeMedia()
                    {
                        Duration = ix["duration"].ToString(),
                        Title = (string)ix["title"],
                        ID = (string)ix["videoinf"]
                    }).ToArray()
                ) { ID = (int)i["id"] }).ToList();//The underlying connection was closed: The connection was closed unexpectedly.

                remotePlaylistArray = new List<Playlist>(_PlaylistArray);
            }

            return _PlaylistArray;
        }

服务代码

    public IEnumerable<Hashtable> GetPlaylistItems(string username, string password, int pid)
    {
        return this.ExecuteHashtable("GetPlaylistItems", username, Crypter.DoTwiceMD5(password), pid);
    }

    public IEnumerable<Hashtable> GetPlaylists(string username, string password, int userid, string filter)
    {
        List<Hashtable> list = this.ExecuteHashtable("GetPlaylists", username, Crypter.DoTwiceMD5(password), userid, filter).ToList();

        foreach (var item in list)
        {
            Hashtable[] arr = GetPlaylistItems(username, password, (int)item["id"]).ToArray();
            item.Add("medias", arr.Length != 0 ? arr : null);
        }

        return list;
    }

异常来自tracelistener是

不应使用数据协定名称“ArrayOfArrayOfKeyValueOfanyTypeanyType:http://schemas.microsoft.com/2003/10/Serialization/Arrays”键入“System.Collections.Hashtable[]”。考虑使用 DataContractResolver 或将任何静态未知的类型添加到已知类型列表中 - 例如,通过使用 KnownTypeAttribute 属性或将它们添加到传递给 DataContractSerializer 的已知类型列表中。

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    在您的情况下发生的情况是 HashTable 不能直接序列化,因此如果您确实需要返回 HashTable,则必须编写自定义序列化程序。

    一个问题:您的服务器端知道播放列表类型吗?如果是,我强烈建议您返回 List 而不是 HashTable ...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 2015-04-16
      • 1970-01-01
      相关资源
      最近更新 更多