可能重复:
VS2003 Web Reference for a WCF Service has Extra "IdSpecified" Parameter
谢谢大家。我完全错过了。是的,班级名称很糟糕。
我有一个Web服务(ASMX),它返回一个包含数组的对象。
我首先定义我的响应对象:
APIResponse response = new APIResponse();
填写数据表:
dtHolder = SqlHelper.ExecuteDatatable(connstring, CommandType.StoredProcedure, strSelect, aParams);
为我的查询结果创建一个持有者:
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++;
}
并使用上面刚刚填充的holder数组填充我要返回的数组(bkgDetTable):
response.bkgDetTable = BkgDetRec_Array;
当我捕获输出时,我看到没有创建整数字段,即使我单步执行代码时,它们也会填充实数。对象API036RsBkgDetRec也将它们定义为整数值。只有定义为字符串的那些才会出现。
<bkgDetTable xmlns="http://sdfsd/xsd">
<eqoEqhtId>96</eqoEqhtId>
<eqoEqszId>40</eqoEqszId>
<eqoEqtpId>DR</eqoEqtpId>
</bkgDetTable>
以下是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;
}
}
}
有什么想法吗?
答案 0 :(得分:2)
对于所有INT
字段,您有两个字段:
实际的int
字段,我确定您已设置为正确的值(private int emptyTallyField;
)
但您还有一个名为private bool emptyTallyFieldSpecified;
的字段,必须设置为true
- 否则您设置的int值< strong>不会序列化
原因是因为对于像string
字段这样的引用类型字段,您可以使用NULL
来表示“没有值”方面,并且您可以使用空字符串或其中的任何其他字符串。
对于值类型,例如int
- 您如何表示“没有指定值”的情况?如果将int字段设置为0 - 那么0是因为你想要“零”作为值,或者是“0”因为你真的想说“没有价值”。因此,所有值类型都具有此额外的(fieldname)specified
属性,该属性指示是否存在实际值。如果没有设置指定标志(默认情况下没有设置),那么你就没有值 - 因此没有任何序列化。
答案 1 :(得分:1)
您会注意到,对于每个属性值类型(integer,double,DateTime,...),您都有XXXSpecified
布尔属性。例如,你有:
public int maxEmpty
和
public bool maxEmptySpecified
使用wsdl.exe(或在Visual Studio中添加Web引用)导入WCF Web服务时会生成这些属性。每次设置相应的值类型属性值时,都需要将它们设置为true
。
我同意这是彻头彻尾的废话但是,就是这样。 ASMX现已弃用。