peter
peter

Reputation: 13501

What JSON Should I be Using For WCF Service

I am running a WCF service with these methods,

public string UploadInspections(Inspection[] inspections)
public string UploadInspection(Inspection inspection)

[DataContract]
public partial class Inspection
{
    [DataMember]
    public DateTime DateTime { get; set; }

    [DataMember]
    public int  Id { get; set; }

    [DataMember]
    public string Comment { get; set; }

    [DataMember]
    public int Rating { get; set; }
}

From javascript I tried to call a POST on these methods using JSON. The JSON I had for the UploadInspection method was this,

{"Id":10,"Comment":"New One","Rating":3}

The UploadInspection method was called, but the inspection object was set to null.

I wasn't sure how to specify the Date field using JSON, and I thought that perhaps the parser didn't like JSON with no Date field. I removed the Date field from the Inspection object, but the same thing happened.

Also what should the JSON look like for the UploadInspections method which is an array? I had some JSON that I tried,

"inspections": [{"Id":10,"Comment":"New One","Rating":3}]

And also this,

[{"Id":10,"Comment":"New One","Rating":3}, {"Id":11,"Comment":"New Two","Rating":2}]

But I was getting this error,

OperationFormatter encountered an invalid Message body. Expected to find an attribute with name 'type' and value 'object'. Found value 'string'.

Upvotes: 0

Views: 3540

Answers (1)

peter
peter

Reputation: 13501

The problem is not what I thought it was, in my service definition it initially looked like this,

[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
string UploadInspections(Inspection[] inspections);

Once I removed this,

BodyStyle = WebMessageBodyStyle.WrappedRequest

It worked!

Upvotes: 9

Related Questions