特殊字符的WCF服务失败

时间:2014-03-04 12:53:59

标签: c# .net wcf character-encoding

我运行的C#WCF服务需要接受ISO-8859-1中的消息。一切正常,直到有人发送包含特殊字符的消息,例如äö

如果字符已编码(ä\344ä),一切正常,但发件人坚持认为如果邮件是ISO-8859-1,则应该有不需要逃避。

更具体地说,这是有效的(简化示例):

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    a
  </soap:Body>
</soap:Envelope>

(但当然由于语法无效而引发另一个异常,但传输本身很好)。

这不起作用:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    ä
  </soap:Body>
</soap:Envelope>

并抛出了一堆无处可寻的神秘错误信息:

<ExceptionType>System.ServiceModel.CommunicationException, System.ServiceModel, ...</ExceptionType>
<Message>The I/O operation has been aborted because of either a thread exit or an application request</Message>
...
<ExceptionType>System.ServiceModel.CommunicationException, System.ServiceModel, ...</ExceptionType>
<Message>An operation was attempted on a nonexistent network connection</Message>

我尝试使用空的App.config运行该服务并且问题仍然存在,所以我很确定问题不在于我创建服务的方式,而在于WCF如何处理消息。将编码更改为UTF-8也无济于事。

任何想法为什么单个变音符号会破坏服务如此灾难性? XML应该支持除ASCII之外的编码,那么这里的交易是什么?我可以做任何改变,或者如果所有的希望都失去了,我怎么能说服发件人编码他们的特殊字符呢?

1 个答案:

答案 0 :(得分:0)

问题不是我想的那样。

客户发送给我们的数据缺少正确的xml声明:

<?xml version="1.1" encoding="ISO-8859-1"?>

有了这个,该服务正常运作。

但我得到的例外是由我们测试连接的程序引起的。该程序是WebServiceStudio,当我们尝试通过它发送特殊字符(特别是ä)时它完全失败:当使用WireShark检查时,消息正文完全没有。

所以,最初我们认为客户遇到了与我们相同的问题,但是当我们注意到我们的测试失败时,很快就找到了真正的原因,这是SOAP消息中缺少的XML声明。