2013-05-30 45 views
2

我的web服务正在返回所有字符串变量。现在我想修改我的服务,以便它可以返回HashTable对象。这是我的WebService的入口点的签名(方法):如何从WebService返回一个HashTable?

public void getPrevAttempts(string fbUserId, string studentId, string assessmentId, out string isAuthenticated, out HashTable attempts) 

在哈希表中的记录被插入形式的SQL查询的结果。每当我尝试运行我的服务时,我都会被重定向到accessdenied.htm页面(因为我的Web.config有这个条目<customErrors mode="On" defaultRedirect="accessdenied.htm"/>)。有什么办法可以返回HashTable或SQL查询的结果吗?

更新:

例外:The type System.Collections.Hashtable is not supported because it implements IDictionary.

+1

什么是服务返回的错误反序列化? – Talha

+5

'HashTable',90年代再次... – gdoron

+1

我非常肯定'out'参数在Web服务签名中不受支持。你也可能会遇到序列化问题,特别是跨平台的问题(一个字符串是一个众所周知的类型,但是.Net Hashtable不是)。然而,一个Hashtable *被标记为可序列化的,所以我会尝试的第一件事就是使用它作为返回值。 –

回答

2

将数据序列化为JSON字符串并返回给客户端。

var serializer = new JavaScriptSerializer(); 
HashTable ht = New HashTable(); 
//Populate ht. 
response = serializer.Serialize(ht) // This will serialize the data to JSON format. 

在客户端,使用

httpResponse = JSON.parse(response); //httpResponse will have key value pair data as you created in server. 
1

我想你应该创建自己的自定义对象或创建一个数组。 你可以将它转换回HashTable。 使用out参数也不是一个好主意。 最好用IsAuthenticated和Attempts创建一个自定义类作为属性。

1

你可以很容易地转换HashTableList<KeyValuePair<string, string>>并返回......

,而不是返回的数据结构,思归数据 - 并在客户端上重建数据结构。

相关问题