代码之家  ›  专栏  ›  技术社区  ›  Frode Lillerud

反序列化WCF服务时更改区域性

  •  1
  • Frode Lillerud  · 技术社区  · 17 年前

    我正在使用C#从Java Web服务读取数据。

    "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."
    

    [编辑]以下是完整的堆栈跟踪:

    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”)并得到了相同的错误。通过设置

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

    在我编写Decimal.Parse(“123.99”)的测试代码之前,我已经开始工作了。

    但是,在调用WCFService方法之前设置CurrentCulture没有任何区别。

    有什么方法可以让我的WCFService代理对象理解返回的XML在另一个cultureformat中吗?

    4 回复  |  直到 17 年前
        1
  •  2
  •   Justin Grant    9 年前

    IClientMessageFormatter.DeserializeReply() 实施WCF充满了扩展性点,因此通常很难知道选择哪一个,但反序列化似乎是这项工作的合适工具。

    看看 this blog post-- sorry its original site is offline so pointing to Internet Archive version

    以下是该博客的代码摘录:

    public object DeserializeReply(Message message, object[] parameters)
    {
        object helperInstance = Activator.CreateInstance(_return);
    
        //we have special condition where service sets Http response code to 403 that signals that an error has occured 
        KeyValuePair<string,object> serviceErrorProperty = message.Properties.FirstOrDefault(p => p.Key == ResponseErrorKey);
        if (serviceErrorProperty.Key != null)
        {
            //we have an error message
            IResponseErrorProvider responseErrorProvider = helperInstance as IResponseErrorProvider;
            if (responseErrorProvider != null)
            {
                //unpack the error payload from message and assign to the object
                ResponseError payload = message.GetBody<ResponseError>();
                responseErrorProvider.ServiceError = payload;
    
                //return fixed null type with error attached to it
                return helperInstance;
            }
        }
    
        //another message we might get is <nil-classes type="array"/> for empty arrays.
        XmlDictionaryReader xdr = message.GetReaderAtBodyContents();
        xdr.MoveToContent();
    
        if (xdr.Name == NullMessage)
        {
            return helperInstance; //standin for the null value
        }
    
        return _formatter.DeserializeReply(message, parameters);
    }
    
    public Message SerializeRequest(MessageVersion messageVersion, object[] parameters)
    {
        return _formatter.SerializeRequest(messageVersion, parameters);
    }
    
        2
  •  1
  •   Shiraz Bhaiji    17 年前

    可以将区域性初始值设定项附加到行为,然后将其添加到端点。这将在反序列化响应的线程上设置区域性,因此应正确反序列化字段。

    有关这方面的代码示例,请查看以下链接:

    http://blogs.msdn.com/drnick/archive/2008/02/26/using-call-context-initializers-for-culture.aspx

        3
  •  0
  •   Marc Wittke    17 年前

    如果文化真的是问题所在,我会非常惊讶。XML规范说小数使用点作为分隔符,(反)序列化程序知道这一点。你能追踪信息,看看941号位置有什么吗?(顺便说一句:我喜欢 SoapUI 对于这样的事情,除了WCF跟踪之外,当涉及到小数时,不同的精度可能是一个问题:如果.net十进制可以处理三分之二的小数,Java服务可能会在后面的7之前放置更多的6666?

        4
  •  0
  •   Marc Wittke    17 年前

    this link 这让amazon服务有点遗憾,它可能会将单位转换为十进制值(顺便说一下:WTF!?)。但是,对于如何在使用IClientMessageInspector接口将原始消息传递给反序列化之前与原始消息进行交互,有一个很好的描述。如果这种奇怪的情况是你的问题,你可以强迫服务提供商遵守他自己的合同,或者做这样的把戏(如果提供商不在你的控制范围内,并说“这是故意的”)。