When I created a nested hastable in wcf, I get an error which is “The underlying connection was closed: The connection was closed unexpectedly.”
I don’t know where is the problem.
Client code
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;
}
Service Code
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;
}
the exception comes from tracelistener is
Type ‘System.Collections.Hashtable[]’ with data contract name ‘ArrayOfArrayOfKeyValueOfanyTypeanyType:http://schemas.microsoft.com/2003/10/Serialization/Arrays’ is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types – for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
What happens in your case is that HashTable is not directly serializable, so if it’s really necessary for you to return a HashTable you have to write a custom serializer.
One question: does your server side know the Playlist type? If yes, I would highly recommend you to return a List instead of a HashTable …