I am trying to create a WCF Json Rest and consume the same in WPF Phone 8.1 App (not silverlight).
My WCF is:
<OperationContract()>
<WebGet(UriTemplate:="getdata", ResponseFormat:=WebMessageFormat.Json)>
Function DoWork() As Dictionary(Of Integer, String)
WCF Code:
Public Class BasicService
Implements IBasicService
Public Function DoWork() As Dictionary(Of Integer, String) Implements IBasicService.DoWork
Dim k As New Dictionary(Of Integer, String)
k.Add(1, "One")
k.Add(2, "Two")
Return k
End Function
End Class
Phone Consumption Code:
Dim httpCli As New System.Net.Http.HttpClient()
Dim httpres As Task(Of HttpResponseMessage) = httpCli.GetAsync("http://localhost:4149/BasicService.svc/getdata")
Dim tk As Task(Of String)
tk = httpres.Result.Content.ReadAsStringAsync
Try
Dim resultstring As String = tk.Result.Substring(tk.Result.IndexOf("{"), tk.Result.LastIndexOf("}") + 1 - tk.Result.IndexOf("{"))
Dim DoWorkResult As Dictionary(Of Integer, String) = Newtonsoft.Json.JsonConvert.DeserializeObject(resultstring)
Catch ex As Exception
End Try
Try
Dim DoWorkResult As Dictionary(Of Integer, String) = Newtonsoft.Json.JsonConvert.DeserializeObject(tk.Result)
Catch ex As Exception
End Try
The Fiddler Data for WCF is : RAW:
HTTP/1.1 200 OK Cache-Control: private Content-Type: application/json; charset=utf-8 Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?RTpcUmFnaGF2YVxpbXBcUHJvamVjdHNcTUNvbGxlY3RvclxNQ1dDRlxCYXNpY1NlcnZpY2Uuc3ZjXGdldGRhdGE=?= X-Powered-By: ASP.NET Date: Mon, 15 Jun 2015 22:50:53 GMT Content-Length: 49
[{"Key":1,"Value":"One"},{"Key":2,"Value":"Two"}]
WebView:
[{"Key":1,"Value":"One"},{"Key":2,"Value":"Two"}]
When deserializing in code:
Error at first Try Catch with result text from '{' to '}' : Additional text encountered after finished reading JSON content: ,. Path '', line 1, position 23.
Error at second Try Catch with not modified json string:
Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'System.Collections.Generic.Dictionary`2[System.Int32,System.String]'.
Could you please correct me where I went wrong or what mistake did I do.