Why fields with additional field “Specified” are a

2019-06-17 05:47发布

问题:

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.

回答1:

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.