如何将TimeSpan对象传递给使用JSON一个WCF方法 [英] How to pass a TimeSpan object to a WCF method using JSON

查看:269
本文介绍了如何将TimeSpan对象传递给使用JSON一个WCF方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种方法使用JSON调用WCF方法,并通过一个时间跨度参数,但我总是收到来自该服务的错误的请求的响应。

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.

下面是一个片段code服务接口:

Here is a snippet code service interface:

 [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
    TimeSpan GetTimeSpan(TimeSpan value);

服务电话:

  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.

响应:

远程服务器返回错误:(400)错误的请求。

The remote server returned an error: (400) Bad Request.

难道我传递错误的时间跨度JSON重新presentation?或者是有没有办法来定义如何反序列化的时间跨度当服务处理请求?

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?

在此先感谢

推荐答案

时间跨度使用WCF的格式是不一样的人用的Newtonsoft JSON序列(JSON 。净) 。您可以使用 DataContractJsonSerializer (DCJS)连载1时间跨度实例,这将是一个WCF REST服务要求的格式。

The format for TimeSpan used by WCF is not the same one used by the Newtonsoft JSON serializer (JSON.NET) . You can serialize one TimeSpan instance using the DataContractJsonSerializer (DCJS), and that will be the format required by a WCF REST service.

在code以下会打印出一些时间跨度值系列化双方JSON.NET和DCJS格式:

The code below will print out the formats of some TimeSpan values as serialized by both JSON.NET and DCJS:

public class StackOverflow_7178839
{
    public static void Test()
    {
        foreach (var ts in new TimeSpan[] { new TimeSpan(23, 59, 59), new TimeSpan(3, 4, 5, 6), new TimeSpan(-4, 3, 4, 5, 333) })
        {
            Console.WriteLine("For TimeSpan value: {0}", ts);
            Console.WriteLine("  {0}", Newtonsoft.Json.JsonConvert.SerializeObject(ts));
            DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(TimeSpan));
            MemoryStream ms = new MemoryStream();
            dcjs.WriteObject(ms, ts);
            Console.WriteLine("  {0}", Encoding.UTF8.GetString(ms.ToArray()));
        }
    }
}

这篇关于如何将TimeSpan对象传递给使用JSON一个WCF方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆