The example here (http://stackoverflow.com/questions/835839/client-configuration-to-consume-wcf-json-web-service) is straightforward and simple. I have created a WCF RESTFUL JSON service that returns an array of simple objects. In JavaScript, I have no problem dealing with what is returned, and my Web client therefore works fine.
I want to make this service callable from either web or C# clients so that the data that is exposed can be used in multiple contexts without having to duplicate the code that gets the data.
In my C# code, I have the proxy client, the interface contract, and the config based binding definitions. I share the interface and data code modules between my C# service and my C# client (a simple console app to use as a test verification harness).
On the C# client, the data that is not in array form comes back great. As soon as I make my service return an array of objects, either as a bare array return or as a wrapped property on a simple object, the serializer on the client side silently fails and returns an empty array.
Here is my client code at the top level
// for managed code to call our Ajax/Json incident service, we need to reuse the interface contract,
// and use the ServiceModel.Channels infra to hand-code a proxy client.
public class IncidentClient : ClientBase<IIncidentServices.IGetActiveIncidents>, IIncidentServices.IGetActiveIncidents
{
public incidents GetActiveIncidents(string environmentAbbreviation)
{
return base.Channel.GetActiveIncidents(environmentAbbreviation);
}
}
class Program
{
static void Main(string[] args)
{
IncidentClient client = new IncidentClient();
incidents data = client.GetActiveIncidents("prod");
Console.Write("Call to GetActiveIncidents returned ");
if (null == data)
{
Console.WriteLine("no data (null)");
}
else
{
Console.WriteLine(data.incidentList.Count.ToString() + " rows of incident data.");
}
Console.WriteLine("\nPress any key to continue...");
Console.ReadLine();
}
}
}
When the code runs, it ALWAYS tells me that there were zero rows and the debugger shows me that an empty array returns. I have traced into the messages being exchanged with the WCF trace tools by activating logging, and I can see my data coming back (100 elements in the array.
The tricky part is that the serializer just silently throws the data away - and that is causing me to wonder if I should just abandon the WCF clientBase based proxy and use raw HTTP get, and a separate JSON parser to deal with the data.
My data contract looks like this:
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace Dan.Test.Incident.Data
{
[DataContract(Name="incidents", Namespace="Dan.Test.Incident.Data")]
public class incidents
{
public incidents()
{
data = new List<incidentData>();
}
[DataMember(Name="incidentList")]
private List<incidentData> data;
[IgnoreDataMember]
public List<incidentData> incidentList
{
get {
if (null == data)
{
data = new List<incidentData>();
}
return data;
}
}
}
[DataContract(Name="incidentData", Namespace="Dan.Test.Incident.Data")]
[Serializable]
public class incidentData
{
// define incident members and accessors for read-only get operations
[DataMember(Name = "irNumber")]
private string m_irNumber = null; // the incident identifier as IR12345, etc.
[DataMember(Name = "title")]
private string m_title = null; // the title of the incident
[DataMember(Name = "devname")]
private string m_devname = null; // list of team members who were engaged
[DataMember(Name = "description")]
private string m_description = null; // description of the incident
[DataMember(Name = "startdate")]
private DateTime m_startdate;
[DataMember(Name = "priority")]
private int m_priority = 0;
[DataMember(Name = "environmentID")]
private int m_environmentID = 0;
[DataMember(Name = "status")]
private string m_status;
[DataMember(Name = "enddate")]
private DateTime m_enddate;
public incidentData()
{
}
}
}
My interface definition is
using Dan.Test.Incident.Data;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace IIncidentServices
{
[ServiceContract(Namespace = "Dan.Test.Incident.Data")]
public interface IGetActiveIncidents
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
incidents GetActiveIncidents(string environmentAbbreviation);
}
}
and my config is straightforward as:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<sources>
<source name="System.ServiceModel.MessageLogging" switchValue="Error,ActivityTracing">
<listeners>
<add type="System.Diagnostics.DefaultTraceListener" name="Default">
<filter type="" />
</add>
<add name="ServiceModelMessageLoggingListener">
<filter type="" />
</add>
</listeners>
</source>
<source propagateActivity="true" name="System.ServiceModel" switchValue="Verbose,ActivityTracing">
<listeners>
<add type="System.Diagnostics.DefaultTraceListener" name="Default">
<filter type="" />
</add>
<add name="ServiceModelTraceListener">
<filter type="" />
</add>
</listeners>
</source>
</sources>
<sharedListeners>
<add initializeData="c:\users\danro\documents\visual studio 2012\projects\gadgetactiveincidentservice\consoleapplication1\app_messages.svclog"
type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp">
<filter type="" />
</add>
<add initializeData="c:\users\danro\documents\visual studio 2012\projects\gadgetactiveincidentservice\consoleapplication1\app_tracelog.svclog"
type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
name="ServiceModelTraceListener" traceOutputOptions="Timestamp">
<filter type="" />
</add>
</sharedListeners>
</system.diagnostics>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<diagnostics>
<messageLogging logEntireMessage="true" logMalformedMessages="true"
logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true"
maxMessagesToLog="3000" maxSizeOfMessageToLog="100000" />
<endToEndTracing messageFlowTracing="true" />
</diagnostics>
<bindings>
<webHttpBinding>
<binding name="NewBinding2" openTimeout="00:05:00" receiveTimeout="00:50:00"
sendTimeout="00:05:00" hostNameComparisonMode="WeakWildcard"
maxBufferSize="2000000" maxBufferPoolSize="2000000" maxReceivedMessageSize="2000000"
useDefaultWebProxy="false" contentTypeMapper="">
<readerQuotas maxDepth="32" maxStringContentLength="100000" maxArrayLength="10000"
maxBytesPerRead="2000000" maxNameTableCharCount="100000" />
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="webwcf">
<webHttp defaultBodyStyle="WrappedRequest" defaultOutgoingResponseFormat="Json"
automaticFormatSelectionEnabled="true" faultExceptionEnabled="true" />
</behavior>
</endpointBehaviors>
</behaviors>
<client>
<endpoint address="http://localhost/GadgetActiveIncidentService/ActiveIncidentService.svc"
behaviorConfiguration="webwcf" binding="webHttpBinding" bindingConfiguration="NewBinding2"
contract="IIncidentServices.IGetActiveIncidents" name="ActiveIncidentService" />
</client>
</system.serviceModel>
</configuration>
Looking at my data, this is a short snippet of what comes back - and the only thing that looks strange is the [d] __type row in the data ....
{"d":[{"__type":"incidentData:Dan.Test.Incident.Data",
"description":"My description","devname":"",
"enddate":"\/Date(1357995120000-0800)\/",
"environmentID":10,"irNumber":"IR742989","priority":1,
"startdate":"\/Date(1357973873643-0800)\/",
"status":"closed","title":"A subset of users "},
{"__type":"incidentData:Dan.Test.Incident.Data","description":"second description.",
"devname":"","enddate":"\/Date(1352871180000-0800)\/",
"environmentID":10,"irNumber":"IR595320","priority":2,
"startdate":"\/Date(1352758680000-0800)\/",
"status":"This incident has been downgraded.",
"title":"Users will be unable to upgrade"}]}
Hope someone here can shed light on what I need to do to make this work :)
Thanks in advance
Dan