【问题标题】:Deserializing JSON in WCF throws xml errors in .Net 4.0在 WCF 中反序列化 JSON 会在 .Net 4.0 中引发 xml 错误
【发布时间】:2011-02-19 11:45:46
【问题描述】:

我快疯了,也许有人可以帮我弄清楚发生了什么。我有一个使用 webinvoke 公开函数的 WCF 服务,如下所示:

[OperationContract]
        [WebInvoke(Method = "POST",
           BodyStyle = WebMessageBodyStyle.Wrapped,
           RequestFormat = WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json,
           UriTemplate = "registertokenpost"
        )]

        void RegisterDeviceTokenForYoumiePost(test token);

测试类的数据契约如下所示:

[DataContract(Namespace="Zooma.Test", Name="test", IsReference=true)]
    public class test
    {
        string waarde;
        [DataMember(Name="waarde", Order=0)]
        public string Waarde
        {
            get { return waarde; }
            set { waarde = value; }
        }
    }

当向服务发送以下 json 消息时, { “测试”: { “沃德”:“布拉” } }

跟踪日志给了我错误(如下)。我只用一个字符串而不是数据类型(void RegisterDeviceTokenForYoumiePost(string token); )尝试了这个,但我得到了同样的错误。感谢所有帮助,无法弄清楚。看起来它正在从 json 消息中创建无效的 xml,但我没有在这里进行任何自定义序列化。

The formatter threw an exception while trying to deserialize the message: Error in
 deserializing body of request message for operation 'RegisterDeviceTokenForYoumiePost'. 
Unexpected end of file. **Following elements are not closed**: waarde, test, root.</Message><StackTrace>
   at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message, 
Object[] parameters)

【问题讨论】:

    标签: wcf json serialization xml-serialization .net-4.0


    【解决方案1】:

    服务需要 { "token": { "waarde": "bla" } },而不是 { "test": { "waarde": "bla" } }

    【讨论】:

      猜你喜欢
      • 2015-06-17
      • 2012-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-01
      相关资源
      最近更新 更多