Web service not returning integer data in an object [duplicate]

喜欢而已 提交于 2020-01-15 06:33:28

问题


Possible Duplicate:
VS2003 Web Reference for a WCF Service has Extra "IdSpecified" Parameter

Thanks everyone. I totally missed that. Yes, the class name is awful.

I have a web service (ASMX) that returns an object which contains an array.

I first define my response object:

APIResponse response = new APIResponse();

Fill a datatable:

dtHolder = SqlHelper.ExecuteDatatable(connstring, CommandType.StoredProcedure, strSelect, aParams);

Create a holder for the results of my query:

    API036RsBkgDetRec[] BkgDetRec_Array = new API036RsBkgDetRec[dtHolder.Rows.Count];
            int i = 0;

            foreach (DataRow row in dtHolder.Rows)
            {
                BkgDetRec_Array[i] = new API036RsBkgDetRec();

                BkgDetRec_Array[i].eqoEqszId = row[0].ToString();
                BkgDetRec_Array[i].eqoEqtpId = row[1].ToString();
                BkgDetRec_Array[i].eqoEqhtId = row[2].ToString();
                BkgDetRec_Array[i].qty = Convert.ToInt32(row[3]);
                BkgDetRec_Array[i].receiveQty = Convert.ToInt32(row[4]);
                BkgDetRec_Array[i].emptyTally = Convert.ToInt32(row[5]);
                BkgDetRec_Array[i].maxEmpty = Convert.ToInt32(row[6]);
                BkgDetRec_Array[i].chsQty = Convert.ToInt32(row[7]);
                BkgDetRec_Array[i].chsTally = Convert.ToInt32(row[8]);

                i++;
            }

and populate the array I am returning (bkgDetTable) with the holder array I just populated above:

response.bkgDetTable = BkgDetRec_Array;

When I capture the output, I see that none of the integer fields are being created even though when I step through the code, they are being populated with real numbers. The object API036RsBkgDetRec also has them defined as integer values. Only the ones defined as string are coming across.

<bkgDetTable xmlns="http://sdfsd/xsd">
<eqoEqhtId>96</eqoEqhtId>
<eqoEqszId>40</eqoEqszId>
<eqoEqtpId>DR</eqoEqtpId>
</bkgDetTable>

Here is the definition of API036RsBkgDetRec:

public partial class API036RsBkgDetRec
{    
private int chsQtyField;

private bool chsQtyFieldSpecified;

private int chsTallyField;

private bool chsTallyFieldSpecified;

private int emptyTallyField;

private bool emptyTallyFieldSpecified;

private string eqoEqhtIdField;

private string eqoEqszIdField;

private string eqoEqtpIdField;

private int maxEmptyField;

private bool maxEmptyFieldSpecified;

private int qtyField;

private bool qtyFieldSpecified;

private int receiveQtyField;

private bool receiveQtyFieldSpecified;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=0)]
public int chsQty
{
    get
    {
        return this.chsQtyField;
    }
    set
    {
        this.chsQtyField = value;
    }
}

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

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=1)]
public int chsTally
{
    get
    {
        return this.chsTallyField;
    }
    set
    {
        this.chsTallyField = value;
    }
}

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

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=2)]
public int emptyTally
{
    get
    {
        return this.emptyTallyField;
    }
    set
    {
        this.emptyTallyField = value;
    }
}

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

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=3)]
public string eqoEqhtId
{
    get
    {
        return this.eqoEqhtIdField;
    }
    set
    {
        this.eqoEqhtIdField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=4)]
public string eqoEqszId
{
    get
    {
        return this.eqoEqszIdField;
    }
    set
    {
        this.eqoEqszIdField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=5)]
public string eqoEqtpId
{
    get
    {
        return this.eqoEqtpIdField;
    }
    set
    {
        this.eqoEqtpIdField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=6)]
public int maxEmpty
{
    get
    {
        return this.maxEmptyField;
    }
    set
    {
        this.maxEmptyField = value;
    }
}

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

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=7)]
public int qty
{
    get
    {
        return this.qtyField;
    }
    set
    {
        this.qtyField = value;
    }
}

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

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=8)]
public int receiveQty
{
    get
    {
        return this.receiveQtyField;
    }
    set
    {
        this.receiveQtyField = value;
    }
}

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

Any ideas?


回答1:


For all the INT fields, you have two fields:

  • the actual int field which I'm sure you're already setting to the proper value (private int emptyTallyField;)

  • but you also have a field called private bool emptyTallyFieldSpecified; which must be set to true - otherwise that int value you set will not be serialized out

The reason for this is because for a reference-type field like a string field, you can have NULL to represent the "there is no value" aspect, and you can have an empty string or any other string in there.

For value types, like int - how else would you represent the "there is no value specified" case? If you set the int field to 0 - is that 0 because you want that "zero" as the value, or is that "0" because you really want to say "there is no value". For this reason, all value types have this additional (fieldname)specified property which indicates whether or not an actual value is present. If that specified-flag is not set (and it's not set by default), then you have no value - thus nothing gets serialized out.




回答2:


You will notice that for each property value type (integer, double, DateTime, ...) you have a XXXSpecified boolean property. For example you have:

public int maxEmpty

and

public bool maxEmptySpecified

Those properties are generated when you import a WCF web service using wsdl.exe (or Add Web Reference in Visual Studio). You need to set them to true every-time you set their corresponding value type property value.

I agree that this is total crap but, that's how it is. ASMX is deprecated now.



来源:https://stackoverflow.com/questions/7263232/web-service-not-returning-integer-data-in-an-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!