反序列化WCF服务时改变文化 [英] Change culture when deserializing WCF service

查看:190
本文介绍了反序列化WCF服务时改变文化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#读取一个Java的web服务的数据。

I'm using C# to read data from a Java-webservice.

我创建在VS2008到服务器的服务引用,并可以调用一个方法,该方法是存在的。然而,一些返回的字段的类型是小数,而作为自动生成的WCF代理获取XML回到失败了的CommunicationException说:

I've created a Service reference in VS2008 to the server, and can call the one method that is there. However, a few of the fields returned are of type Decimal, and as the automatically generated WCF proxy gets XML back it fails with a CommunicationException saying:

"Error in deserializing body of reply message for operation 'getOpenReceivables'."
"There is an error in XML document (1, 941)."
"Input string was not in a correct format."

这是一个完整的堆栈跟踪:

Here is a full stacktrace:

at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt)   
at System.Decimal.Parse(String s, NumberStyles style, IFormatProvider provider)
at System.Xml.XmlConvert.ToDecimal(String s)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderExecutePortType.Read2_XXNG_OPEN_RECEIVABLES(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderExecutePortType.Read3_Item()  
at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer.Deserialize(XmlSerializationReader reader)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)

我可以看到,返回的十进制数格式化为标点符号作为小数点。出于测试目的,我尝试了Decimal.Parse(123.99),并得到了同样的错误。通过设置

I can see that the decimal number returned is formatted with a punctuation as the decimal. For testing purposes I tried a Decimal.Parse("123.99") and got the same error. By setting a

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");



我testcode为Decimal.Parse(123.99)我得到之前的工作。

before my testcode for Decimal.Parse("123.99") I get that working.

不过,调用WCFService方法没有任何区别之前设置的CurrentCulture。

However, setting the CurrentCulture before calling the WCFService method doesn't make any difference.

有什么方法,我可以得到我WCFService代理对象明白,返回的XML是在另一个cultureformat?

Are there any ways I can get my WCFService proxy object to understand that the returned XML is in another cultureformat?

推荐答案

您是否尝试过使用自定义的 IClientMessageFormatter.DeserializeReply()执行? WCF是塞满了扩展点鳃,所以它往往很难知道哪一个选择,但DeserializeReply看起来像这个工作的工具。

Have you tried using a custom IClientMessageFormatter.DeserializeReply() implementation? WCF is stuffed to the gills with extensibility points, so it's often hard to know which one to pick, but DeserializeReply looks like the right tool for this job.

看看的 http://blogs.vertigo.com/personal/petar/Blog/archive/2008/06/23/twitter-wcf-client.aspx 它看起来像类似的事情给你想要做什么:使用WCF客户端调用非WCF Web服务,并做响应的专门加工占服务器上的非WCF样行为。

Take a look at http://blogs.vertigo.com/personal/petar/Blog/archive/2008/06/23/twitter-wcf-client.aspx which looks like a similar thing to what you're trying to do: using a WCF client to call a non-WCF web service, and to do specialized processing of the response to account for non-WCF-like behavior on the server.

这篇关于反序列化WCF服务时改变文化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆