4

I am using WCF RESTful webservice and I am getting this error:

There was an error while trying to deserialize parameter http://tempuri.org/:aa. The InnerException message was 'There was an error deserializing the object of type WcfService1.Test. DateTime content '2014-05-31T18:30:00.000Z' does not start with '/Date(' and end with ')/' as required for JSON.'. Please see InnerException for more details.'. See server logs for more details. The exception stack trace is:`

at System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameterPart(XmlDictionaryReader reader, PartInfo part) at System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameter(XmlDictionaryReader reader, PartInfo part) at System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameters(XmlDictionaryReader reader, PartInfo[] parts, Object[] parameters, PartInfo returnInfo, Object& returnValue) at System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeBodyCore(XmlDictionaryReader reader, Object[] parameters, Boolean isRequest) at

Input from jQuery:

 $.ajax({ url: "Restful.svc/new/one", type: "POST", contentType: "application/json; charset=utf-8", headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, data: JSON.stringify({ aa: { xaaaaaa: "2014-05-31T18:30:00.000Z" } }), dataType: 'json', processData: true, success: function (msg) { alert(msg.helloWorldResult); }, error: function (msg) { var y = 0; } }); 

WCF Service:

 [OperationContract] [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "new/one")] String helloWorld(Test aa); 

Test class:

 public class Test { [JsonProperty(ItemConverterType=typeof(IsoDateTimeConverter))] public DateTime xaaaaaa { get; set; } } 

If I pass the input xaaaaaa as : /Date(new Date.valueOf().toString())/ it takes in. How do I change the default date formatter in the WCF Service to use IsoDateFormat for serialize and deserializing.

I have tried modifying the route table settings but I was not able to find most of the libraries. If I use JSON.NET it says it uses ISO format by default. How do I set it to take it in the WCF web service?

2
  • 1
    Similar post -> stackoverflow.com/questions/11105856/… Commented Jun 6, 2014 at 18:39
  • I did the exact same thing and I had to do trim off the quotes (") once the serializing was done. This was because the DataContract serializer (which is a default serializer in WCF service) serializes the final result and adds quotes around it ( Example: "\"0001-01-01T00:00:00"\", when received on client side ). Thank you @Irb it works now. Commented Jun 12, 2014 at 10:37

1 Answer 1

5

I am using Newtonsoft to serialize the date format to hold ISODate format. I was able to solve my problem doing this:

Test.cs:

[DataContract] public class Test { public DateTime xaaaaaa { get; set; } [DataMember(Name = "xaaaaaa")] private string HiredForSerialization { get; set; } [OnSerializing] void OnSerializing(StreamingContext ctx) { this.HiredForSerialization = JsonConvert.SerializeObject(this.xaaaaaa).Replace('"',' ').Trim(); } [OnDeserialized] void OnDeserialized(StreamingContext ctx) { this.xaaaaaa = DateTime.Parse(this.HiredForSerialization); } } 

jQuery:

$.ajax({ url: "Transfer.svc/new/one", type: "POST", contentType: "application/json; charset=utf-8", headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, data: JSON.stringify({ aa: { xaaaaaa: "2014-05-31T18:30:00.000Z" } }), dataType: 'json', processData: true, success: function (msg) { tester = msg.helloWorldResult; //"2014-06-01T00:00:00+05:30" }, error: function (msg) { var y = 0; } }); 

Date I selected from the date picker (jQuery):

Selected date : 1st-Jun-2014

WCF Service Looks like this:

 [OperationContract] [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "new/one")] public Test helloWorld(Test aa) { return aa; } 

This works great!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.