Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does VS generate a string property for a xs:nonNegativeInteger attribute for WCF client?

I am building a WCF Client with VS2010. The web service we consume defines some data that it returns as xs:nonNegativeInteger. However VS2010, upon generation of the WCF client code, generates classes with properties of type string for these xs:nonNegativeInteger attributes.

I am wondering why that's the case and if and how I can tell VS2010 to adjust its mappings from xs:nonNegativeInteger to integer rather than string.

(I can't change the wsdl of the web service we consume... And I am also hesitant of simply changing the generated code in case we need to update the service reference, so some sort of data type mapping via a config would be ideal.)

Thanks all!

Example snippet of the WSDL we consume:

<xs:element minOccurs="0" name="blub" type="xs:nonNegativeInteger" />

Example snippet of the generated WCF client code:

<System.Xml.Serialization.XmlElementAttribute(DataType:="nonNegativeInteger", Order:=0)>  _

Property blub() As String
like image 718
spse Avatar asked Jul 13 '26 06:07

spse


1 Answers

It seems you've configured SvcUtil to use the XmlSerializer instead of the DataContractSerializer. MSDN says XmlSerializer will serialize the xs:nonNegativeInteger to string while DataContractSerializer will serialize it to Int64 (search for 'nonNegativeInteger' in each page).

If you don't want to use DataContractSerializer for some reason, your best option is to take advantage of the generated proxy classes being partial classes and create a separate property (either int or long) to encapsulate the conversion logic without affecting how WCF serializes/deserializes that field.

BTW: if that string conversion really annoys you, vote up this issue in CodePlex.

like image 77
Sixto Saez Avatar answered Jul 15 '26 22:07

Sixto Saez