I have the following OperationContract on my WCF Service:
public int ProcessEntries(Entry[] entries)
{
return entries.Length;
}
My interface defintion for my OperationContract is as follows:
[OperationContract]
[WebInvoke(
Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
int ProcessEntries(Entry[] entries);
The Entry
class is defined as follows:
[DataContract]
public class Entry
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
}
I'm calling the OperationContract of the WCF Service via jQuery with the following call:
$.ajax({
url: 'MyService.svc/json/ProcessEntries',
type: 'POST',
data: JSON.stringify({
entries: [
{ Id: 1, Name: 'Test1' },
{ Id: 2, Name: 'Test2' }
]
},
contentType: 'application/json',
success: function (d) { console.log(d); }
});
In Chrome Developer Tools, I see my Request Payload as follows:
{
"entries": [
{
"Id": 1,
"Name": "Test1"
},
{
"Id": 2,
"Name": "Test2"
}
]
}
I'm expecting the response to be 2
, but I always receive the response of 0
.
What am I doing wrong here? Why does my OperationContract think that the array of Entry
has a length of 0
?
Edit In Debug mode, I can see that the entries array is actually empty. Why would it not be getting the entries from deserializing the JSON sent in the request?
Final Edit I discovered that adding BodyStyle = WebMessageBodyStyle.WrappedRequest
to my WebInvoke
attribute fixes the problem, but I'm not sure why. Can anyone answer this question?