PDA

View Full Version : WCF json problems



cain@spiral.co.nz
8 May 2012, 10:18 PM
I have built a WCF web service
http://nzrabwcfservice.nzrab.org.nz/ShipperService.svc/json/test/
or http://nzrabwcfservice.nzrab.org.nz/ShipperService.svc/json/?s=test&ab=ab&sdfd
it returns

[{"FirstName":"Test","LastName":"System"},{"FirstName":"test2","LastName":"test2test2"}]

Which looks like it should work to me but I cant get a json p store to open it. As far as I can see in the browser and using fiddler the out put formatting is similar to twitters feed (http://api.twitter.com/1/statuses/user_timeline.json?screen_name=microsoft&include_rts=1) which works perfectly.

Please help

cain@spiral.co.nz
9 May 2012, 3:59 PM
Heres my code
web.config


<?xml version="1.0"?>
<configuration>
<connectionStrings>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
<add assembly="System.Data.Entity.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
</assemblies>
<buildProviders><add extension=".edmx" type="System.Data.Entity.Design.AspNet.EntityDesignerBuildProvider" /></buildProviders></compilation>
</system.web>
<system.serviceModel>
<services>
<service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
</configuration>


service

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Text

Namespace RestService
Public Class RestServiceImpl
Implements IRestServiceImpl

Public Function XMLData(search As String) As List(Of Architect) _
Implements IRestServiceImpl.XMLData

Return ArchitectFromSearch(search)
End Function

Public Function JSONData(search As String) As List(Of Architect) _
Implements IRestServiceImpl.JSONData

Return ArchitectFromSearch(search)
End Function
Public Function JSONData2(search As String) As List(Of Architect) _
Implements IRestServiceImpl.JSONData2
Return ArchitectFromSearch(search)
End Function
Public Function ArchitectFromSearch(search As String) As List(Of Architect)

Dim Archis As New List(Of Architect)
Dim RATableAdapter As New RabTableAdapters.ArchitectsTableAdapter
Dim ta As Rab.ArchitectsDataTable = RATableAdapter.Search(search)
Dim tr As Rab.ArchitectsRow
For Each tr In ta
Dim Archi As New Architect
Archi.FirstName = tr.FirstName
Archi.LastName = tr.LastName
Archis.Add(Archi)
Next
Return Archis
End Function



End Class
End Namespace

interface


Imports System.ServiceModel
Imports System.Runtime.Serialization
Imports System.ServiceModel.Web

Namespace RestService
<ServiceContract()> _
Public Interface IRestServiceImpl

<OperationContract()> _
<WebInvoke(Method:="*", _
RequestFormat:=WebMessageFormat.Json, _
ResponseFormat:=WebMessageFormat.Json, _
BodyStyle:=WebMessageBodyStyle.Bare, _
UriTemplate:="json/{search}/*")> _
Function JSONData(ByVal search As String) As List(Of Architect)

<OperationContract()> _
<WebInvoke(Method:="*", _
RequestFormat:=WebMessageFormat.Json, _
ResponseFormat:=WebMessageFormat.Json, _
BodyStyle:=WebMessageBodyStyle.Bare, _
UriTemplate:="json/?s={search}")> _
Function JSONData2(ByVal search As String) As List(Of Architect)

<OperationContract()> _
<WebInvoke(Method:="GET", _
ResponseFormat:=WebMessageFormat.Xml, _
BodyStyle:=WebMessageBodyStyle.Wrapped, _
UriTemplate:="xml/{search}")> _
Function XMLData(ByVal search As String) As List(Of Architect)

'<OperationContract()> _
'Sub SaveShipper(ByVal shipper As Shipper)

End Interface
<DataContract()> _
Public Class Architect
<DataMember()> _
Public Property FirstName As String

<DataMember()> _
Public Property LastName As String

End Class
End Namespace

cain@spiral.co.nz
9 May 2012, 4:48 PM
this is interesting and very strange
if I copy the twitter feed data from
http://api.twitter.com/1/statuses/user_timeline.json?screen_name=microsoft&include_rts=1
which works fine and create a file
http://nzrabwcfservice.nzrab.org.nz/tweet.json

I have the same problem