Reputation: 904
I am attempting to create a restful web service in dotnet (VS 2012) that accepts a JSON list. There is this very helpful post.
Jquery Ajax Posting json to webservice
The example is spot on, but I need the DataContract sorted. Here is my problem:
Fiddler posts:
{ "Markers": [
{ "position": "128.3657142857143", "markerPosition": "7" },
{ "position": "235.1944023323615", "markerPosition": "19" },
{ "position": "42.5978231292517", "markerPosition": "-3" }
]};
In the VS2012 Debugger I see the equivalent:
{ "Markers": [
{ "position": "0", "markerPosition": "0" },
{ "position": "0", "markerPosition": "0" },
{ "position": "0", "markerPosition": "0" }
]};
Here is the code (straight from the linked example):
public class Marker
{
decimal position { get; set; }
int markerPosition { get; set; }
}
public string CreateMarkers(List<Marker> Markers)
{
return "Received " + Markers.Count + " markers.";
}
Here is the contract:
[OperationContract]
[WebInvoke(UriTemplate = "Markers", Method = "POST", ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
string CreateMarkers(List<Marker> Markers);
This other, equally wonderful, post explains that the datacontract needs to be set to see the parameter values:
WCF REST POST of JSON: Parameter is empty
My original question was what should the data contract be. I started with:
[OperationContract]
[WebInvoke(UriTemplate = "Markers", Method = "POST", ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
string CreateMarkers(List<Marker> Markers);
After which the message structure comes through, but null content. Then, upon suggestion, I've tried:
[OperationContract]
[WebInvoke(UriTemplate = "Markers", Method = "POST", ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
string CreateMarkers(List<Marker> Markers);
After which the message structure and the content were null.
[OperationContract]
[WebInvoke(UriTemplate = "WrappedMarkers", Method = "POST", ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
string WrappedMarkers(MarkerRequest wrappedmarkers);
[DataContract]
public class MarkerRequest
{
[DataMember]
public List<Marker> Markers
{
get { return _markers; }
set { _markers = value; }
}
private List<Marker> _markers = new List<Marker>();
}
public string WrappedMarkers(MarkerRequest wrappedmarkers)
{
return wrappedmarkers.Markers.ToString();
}
Here is the operations contracted, changed to WrappedRequest
The structure was correct, but the content was again null.
Back to square one. I seem to be able to see the structure of the message. How do I get the content mapped to the structure?
Upvotes: 5
Views: 12512
Reputation: 488
As par my knowledge You getting '0' value because of you didn't set [DataContract] and [DataMember] attribute on your data class. After adding those attr, your class should be look like
[DataContract]
public class Marker
{
[DataMember]
decimal position { get; set; }
[DataMember]
int markerPosition { get; set; }
}
Upvotes: 8