WCF中可为空值类型的错误

问题描述 投票:0回答:2

我正面临以下问题

http://msdn.microsoft.com/en-us/library/ms187557.aspx(“输入参数处理”一节,但建议的解决方案似乎没有工作)。似乎传入的NullableInteger始终被视为空字符串而非空值

我创建了一个MessageContract,其中包含Integer的可空类型,如下所示:

<xs:complexType>
    <xsTongue Tiedequence>
:
<xs:element minOccurs="0" maxOccurs="1" name="InputSub1" type="tnsTongue TiedubClass"/>
<xs:element minOccurs="1" maxOccurs="1" name="NullableInteger" nillable="true" type="xs:int"/>
    </xsTongue Tiedequence>
</xs:complexType>

我正在使用SoapUI来测试带有以下SOAP请求的可空整数

<soapenv:Envelope xmlnsTongue Tiedoapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:myw="MyWCFTest">
<soapenv:Header/>
<soapenv:Body>
  <myw:MessageRequest>
     <myw:InputSub1>
        <mywTongue TiedubProperty1>USD</mywTongue TiedubProperty1>
     </myw:InputSub1>
     <myw:NullableInteger myw:nil="true" />
  </myw:MessageRequest>
</soapenv:Body>
</soapenv:Envelope>

但是,它总是抛出错误,并带有以下简短描述:

输入字符串的格式不正确。 System.Xumber.SvertInt32的System.Number.StringToNumber(String str,NumberStyles options,NumberBuffer&number,NumberFormatInfo info,Boolean parseDecimal)处于System.Xml.XmlConvert.ToInt32(String s)的System.Number.ParseInt32(String s,NumberStyles style,NumberFormatInfo info) System.Xml上Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderIServiceDemo1.Read5_MessageRequest()中的Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderIServiceDemo1.Read3_NullableOfInt32(布尔检查类型)位于Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer1.Deserialize(XmlSerializationReader reader)处.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader,String encodingStyle,XmlDeserializationEvents事件)System.FormatException

注意:我面临一个完全相同的问题,当我谷歌我发现这已经发布在这里Error in Nullable Value Types in WCF但几乎没有任何答案。我在这里发布它(StackOverflow)以获得更多关注。

asp.net wcf
2个回答
1
投票

nil属性在您的命名空间xmlns:myw="MyWCFTest中不存在。

它是XML模式命名空间http://www.w3.org/2001/XMLSchema-instance的一部分。

在根元素<soapenv:Envelope>中添加对此的引用,如下所示

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"然后使用该别名引用nil属性,即xsi:nil="true"


0
投票

我找到了一个解决方法来处理代码背后的可空

    [XmlIgnore]
    public int? NullableIntegerCount{ get; set; }

    [XmlElement("NullableInteger")]
    public string NullableIntegerText
    {
        get { return this.NumberOfPagesCount.HasValue ? this.NullableIntegerCount.Value.ToString("F2") : string.Empty; }
        set
        {
            if (!string.IsNullOrEmpty(value))
            {
                this.NullableIntegerCount= Convert.ToInt32(value);
            }
            else
            {
                this.NullableIntegerCount= null;
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.