Why fields with additional field "Specified" are always null?
Asked Answered
S

1

9

I have the app. Here I generate client service from WSDL. Nowdays some functions work normal. But some are wrong.

It is a part from WSDL

<xs:complexType name="TStartInfoCalcZoneViewForArea">
   <xs:sequence>
      <xs:element minOccurs="0" name="ID" type="xs:int"/>
      <xs:element minOccurs="0" name="startFreq" type="xs:double"/>
      <xs:element minOccurs="0" name="endFreq" type="xs:double"/>
      <xs:element minOccurs="0" name="startTime" type="xs:string"/>

It is a part in c#

public partial class TStartInfoCalcZoneViewForArea
{

    private int idField;

    private bool idFieldSpecified;

    private double startFreqField;

    private bool startFreqFieldSpecified;

    private double endFreqField;

    private bool endFreqFieldSpecified;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=0)]
public int ID
{
    get
    {
        return this.idField;
    }
    set
    {
        this.idField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool IDSpecified
{
    get
    {
        return this.idFieldSpecified;
    }
    set
    {
        this.idFieldSpecified = value;
    }
}

I set value to this fields. For example

.ID = 100;
.IDSpecified = true; // I set nothing, false. But result is same.

The problem is that all this fields (ID,endFreq,startFreq) are null in gsoap server.

What is the reason of this problem? How can I fix it?

Update - the reason and solution

The problem was: I do not have the source code of the "gsoap server". But on the agreement in our company we use (can watch) the log from this application. This log was incorrect (First of all, there are no log messages in this situation. Then this messages were incorrect. After fix the problem was solved).

Also there are a lot classes and structures with "double" field. So in instances of some classes I set "...Specified = true;". In another cases I do not set "...Specified = true;". After the log was fixed, I see the problem.

So I need set "...Specified = true;" in all classes. I do not know is this solution correct, because

1) I ask another progammers in our company, but they do not know wcf normal.

2) set "...Specified = true;", but I see in log the same message.

Shrum answered 22/10, 2013 at 13:24 Comment(0)
B
27

The problem is this: your field idField is of type int, so in .NET, it cannot be null - it will always have to have a valid integer value, e.g. 0.

On the other hand, the XML schema defines it as optional:

<xs:element minOccurs="0" name="ID" type="xs:int"/>

So there's no way that the .NET client can know whether that value of 0 in your idField means that there is no value defined (since it has a minOccurs=0), or whether you really mean to send the value 0 to the server.

That's where the idFieldSpecified comes into play:

  • if idField is 0 and idFieldSpecified is false --> then no value was defined (e.g. a bit like NULL in SQL)

  • if idField is 0 and idFieldSpecified is true --> then you really want to send the 0 value to the caller

So if you have fields that have an accompanying (field)Specified field, if you want to actually send a value, then you must set the (field)Specified value to true - otherwise the value set is NOT sent.

Barrack answered 22/10, 2013 at 13:40 Comment(9)
Thanks. My problem that is I set all values. ID = 100 startFreq = 1500 endFreq = 2500; But in gsoap application this fields are null (other fields are good)Shrum
@novicegis: and did you set their accompanying ....specified field to TRUE ???? If you don't set those fields to TRUE, then your values WILL NOT be serialized into the message!Barrack
Well, then maybe that gsoap system has some issues with those SOAP messages you send it ....Barrack
Have you tried to set up WCF Message Logging so that you could see what messages are sent to gsoap?Barrack
no. I not log it. I always have the same error in log, which was created by another person (i do not have the source code of c++ application). But the reasons were different. Now the problem is solved.Shrum
@novicegis: I'm glad it's solved. Maybe you could update your question with the solution - someone else might benefit from that, if they happen to run into the same problem ...Barrack
This is a horrible design. Why doesn't WCF see the minOccurs="0" and generate a Nullable<int> instead?Microbicide
@simonlchilds: maybe because WCF needs to be interoperable and cannot expect all clients (incluing Ruby, PHP, VBA - and more!) to have support for nullable types.....Barrack
@Barrack I agree with that, what I mean is, when consuming a SOAP client in .NET, where there is support for nullable types, why doesn't .NET generate a nullable type instead of all this ...Specified nonsense. It's just another thing that makes WCF a pig to work with.Microbicide

© 2022 - 2024 — McMap. All rights reserved.