Zero values ​​for object properties de-serialized by WCF

I have a WCF web service that worked fine. Somewhere along the line it stopped, and I cannot understand why. The code and interface never changed or made web.config (at least not with respect 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; } } 

And the service interface is simple:

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

The second two parameters strvalue and intvalue were added only for troubleshooting, to make sure they get deserialized - and they are. When I got into the service, I get an error saying that I am missing the Category1 parameter from the request, but, as you can see this Fiddler screenshot, there are values.

fiddler screenshot

I can get primitive values, but the objects seem to all be created with null values ​​or default values. What am I doing wrong?

UPDATE

In fact, I did not have an answer to my original question, which sucks, but Sixto suggested switching my serialization to JSON. JSON was the original design, but was muffled when I had problems with it. After I successfully switched to JSON, everything was correctly serialized and deserialized. Now I just wait for this to break without explanation, so I can return to XML ....

+7
source share
2 answers

The DataMember IsRequired attribute tells the WCF deserializer to expect an element in an XML message with that name (at least for soap deserialization). It seems that the client code generating the message request no longer sends all Category elements .... This does not mean that you cannot send a null value for the DataMember marked with IsRequired, it just means that the element must exist in the message.

UPDATE: more closely, looking at XML, should there not be a QuizServiceArgs element in XML?

+5
source

You have a problem with namespace. By default, when creating an interface for a servicecontract, it assigns a namespace to it. The namespace is similar to a scope for SOAP XML elements, and if it does not fall within the same scope, it considers that it does not exist. Most likely, this is the code in which messages stop providing a namespace (?). You should refer to it when publishing the XML code, but I'm not quite sure what it assigns (something specific to the server), so it is recommended that you always define a namespace, for example:

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

And then the messages should have a namespace in the SOAP request.

 <Save xmlns="your namespace">.....</Save>
<Save xmlns="your namespace">.....</Save> 

The namespace must also match the service declaration in web.config.

Also need this in datacontract

[DataContract(Namespace = "your namespace")]

+8
source

All Articles