I'm trying to find a way to call a WCF method using JSON and pass a TimeSpan as parameter but I always receive a "Bad request" response from the service.
Here is a snippet code service interface:
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
TimeSpan GetTimeSpan(TimeSpan value);
Service call:
String method = "GetTimeSpan";
String result = null;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + method);
request.KeepAlive = false;
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "application/json";
JsonSerializer serializer = new JsonSerializer();
TimeSpan ts = new TimeSpan(23, 59, 59);
String jsonString = JsonConvert.SerializeObject(ts);
String data = jsonString; //jsonString = "23:59:59"
//I have already tryed with {"value": "23:59:59"} and the result is the same, bad request.
request.ContentLength = data.Length;
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(data);
writer.Close();
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
result = reader.ReadToEnd();
response.Close();
This is just an example. When calling a service without TimeSpan everything works fine. I need to put it work in order do keep compatibility with other clients that are consuming the service in the typical way.
Response:
The remote server returned an error: (400) Bad Request.
Am I passing the wrong TimeSpan json representation? Or is there a way to define how to deserialize the TimeSpan when the service handles the request?
Thanks in advance