【问题标题】:Pass a JSON array to a WCF web service将 JSON 数组传递给 WCF Web 服务
【发布时间】:2011-02-02 22:49:02
【问题描述】:

我正在尝试将 JSON 数组传递给 WCF 服务。但这似乎不起作用。我实际上从服务中提取了一个数组 [GetStudents] 并将完全相同的数组发送回服务 [SaveStudents] 并且没有收到任何内容(空数组)。 JSON 数组的格式为:

[
  {"Name":"John","Age":12},
  {"Name":"Jane","Age":11},
  {"Name":"Bill","Age":12}
]

合同格式如下:

//Contracts
[DataContract]
public class Student{
  [DataMember]public string Name { get; set; }
  [DataMember]public int Age{ get; set; }
}

[CollectionDataContract(Namespace = "")]
public class Students : List<Student>
{
  [DataMember]public Endorsements() { }
  [DataMember]public Endorsements(IEnumerable<Student> source) : base(source) { }
}

//Operations
public Students GetStudents()
{
  var result = new Students();
  result.Add(new Student(){Name="John",12});
  result.Add(new Student(){Name="Jane",11});
  result.Add(new Student(){Name="Bill",12});
  return result;
}

//Operations
public void SaveStudents(Students list)
{
  Console.WriteLine(list.Count); //It always returns zero
}

是否有一种特殊的方式可以将数组发送到 WCF REST 服务?

【问题讨论】:

    标签: asp.net wcf json web-services wcf-rest


    【解决方案1】:

    我有类似的问题。 我从浏览器调用该服务,问题是 Firefox 将请求内容类型从“application/json”动态更改为“application-json;charset=utf-8”。 如果您从浏览器调用服务,请使用非 firefox 浏览器对其进行测试,如果是这种情况,您需要从请求内容类型标头中删除字符集

    【讨论】:

    • @Ash:这正是我的问题。该服务可在 Google Chrome 中运行,但在 FireFox 中失败。是否有任何解决方法:我无法控制 REST 服务!
    • 我找不到客户端解决方案。 xml 类型 (application/xml) 是一个选项吗?
    • 我的 WCF 4.0 服务即使在 Firefox 中也能正常工作(根据 FireBug)。也许它在 3.5 中被破坏了?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-06
    • 2015-06-03
    • 1970-01-01
    • 2019-03-26
    相关资源
    最近更新 更多