为对象的属性空值由WCF反序列化 [英] Null values for object properties de-serialized by WCF

查看:295
本文介绍了为对象的属性空值由WCF反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WCF Web服务使用的做工精细。某处线停了,我不能告诉原因。该代码和接口从未改变也没有在web.config(至少在不涉及Web服务部分)。我有一个类:

I have a WCF web service that used to work fine. Somewhere down the line it stopped and I cant tell why. The code and the interface never changed nor did the web.config (at least not in relation to the web services section). I have a class:

[DataContract]
public class QuizServiceArgs
{
    [DataMember(IsRequired = true, Order = 1)] 
    public int Category1 { get; set; }

    [DataMember(IsRequired = true, Order = 2)] 
    public int Category2 { get; set; }

    [DataMember(IsRequired = true, Order = 3)] 
    public int Category3 { get; set; }

    [DataMember(IsRequired = true, Order = 4)] 
    public int Category4 { get; set; }
}

和服务接口非常简单:

public interface IQuizService
{
    [OperationContract]
    [WebInvoke(Method = "POST",
       BodyStyle = WebMessageBodyStyle.WrappedRequest,
       ResponseFormat = WebMessageFormat.Json)]
    ServiceResult Save(QuizServiceArgs answers, string strvalue, int intvalue);
}



第二两个参数 strValue的的intValue 添加仅用于故障排除,看看那些都拿到反序列化 - 他们。当我打的服务,我得到一个错误,说我失踪请求中的组别参数,但你可以看到这个提琴手截图,该值在那里。

The second two params strvalue and intvalue were added only for troubleshooting to see if those were getting deserialized -- and they are. When I hit the service, I get an error saying that I'm missing the Category1 parameter from the request but as you can see this Fiddler screenshot, the values are there.

我能得到的原始值传递的对象,但似乎所有空或默认值被实例化。我究竟做错了什么?

我从来没有真正得到了我原来的问题回答了这很烂,但西斯托建议,将我的序列化到JSON。 JSON是原来的设计,但是当我在和就倒霉了nixed。我成功切换回JSON之后,一切都被序列化和反序列化正常。现在我只是在等待这打破没有解释这样我就可以切换回XML ....

I never actually got my original question answered which sucks, but Sixto suggested that I switch my serialization to JSON. JSON was the original design but got nixed when I was having trouble with it. After I successfully switched back to JSON, everything was serializing and deserializing properly. Now I am just waiting for this to break for no explanation so I can switch back to XML....

推荐答案

将DataMember IsRequired 属性告诉WCF解串器预计将在与那名(至少它为肥皂反序列化)消息的XML元素。这听起来像正在生成消息请求不再发送所有类别...元素的客户端代码。它并不意味着你不能发送一个空值的数据成员标记IsRequired,它只是意味着该元件需要在邮件中存在

The DataMember IsRequired attribute tells the WCF deserializer to expected an element in the message XML with that name (at least it does for soap deserialization). It sounds like the client code that is generating the message request is no longer sending all the Category... elements. It doesn't mean you can't send a null value for a DataMember marked IsRequired, it just means the element needs to exist in the message.

更新:在看在XML更加紧密,不应该有在一个XML元素QuizServiceArgs?

UPDATE: looking at the XML more closely, shouldn't there be a QuizServiceArgs element in the XML?

这篇关于为对象的属性空值由WCF反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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