I have a RESTful web service that returns JSON-serialized data. It can successfully serialize a Dictionary<string, string> object, but I would like each function to be able to return Dictionary<string, object>.
When I return Dictionary<string, string>, I get the expected JSON response. But when I try to return Dictionary<string, object>, I get this response:
ReadResponse() failed: The server did not return a response for this request.
So, ON TO THE CODE! Here is the Dictionary<string, object> code that fails:
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public Dictionary<string, object> Test(String Token, String Id)
{
Dictionary<string, object> testresults = new Dictionary<string, object>();
testresults.Add("Test1Key", "Test1Value");
Dictionary<string, string> innertestresults = new Dictionary<string, string>();
innertestresults.Add("InnerTest1Key", "InnerTest1Value");
innertestresults.Add("InnerTest2Key", "InnerTest2Value");
testresults.Add("Test2Key", innertestresults);
return testresults;
}
And, just for kicks/reference, here is the Dictionary<string,string> code that works perfectly:
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public Dictionary<string, string> Test(String Token, String Id)
{
Dictionary<string, string> testresults = new Dictionary<string, string>();
testresults.Add("Test1Key", "Test1Value");
testresults.Add("Test2Key", "Test2Value");
testresults.Add("Test3Key", "Test3Value");
return testresults;
}
If anybody has any ideas of how to get this to work (or any alternative ways of doing this to get the same end result), please do let me know! I'm pretty open on how to do this.
On the topic of usage... the reason I need the mix is so that I can return results like this (where the "Data" part could be ANYTHING... not necessarily something with the keys ID, Type, and MaxUsers):
{"Status":"Success","Data":{"ID":"1234","Type":"Live","MaxUsers":"5"}}
{"Status":"Failure","Error":"ID does not exist"}
Thank you all very much!