为什么WCF默认不支持UTF-8和UTF-16以外的其他编码?
我们发布并由我们的一位客户使用的 WCF SOAP 服务存在一些问题。问题在于客户发出编码为 ISO-8859-1 的 SOAP 请求,这会导致错误消息:
HTTP/1.1 415 无法处理消息,因为内容类型“text/xml;charset=iso-8859-1”不是预期类型“text/xml;”字符集=utf-8'。
显然,WCF 仅支持 UTF-8 和 UTF-16 大端,根据 http://msdn.microsoft.com/en-us/library/ms751486(v=VS.90).aspx。这个问题也在以下问题中讨论:
虽然我同意 unicode 是首选,但我无法强迫我们的客户更改他们的客户端,因此我必须修复我们的服务。
让我烦恼的是,我需要将一些 MSDN 作者编写的超过 150 行代码放入我的项目中 - 我无法维护该代码或对其负责。我还花了相当多的时间在 Google 上寻找解决方案并尝试理解它们,我觉得这对于 WCF 可以开箱即用处理的事情来说是浪费时间。
我的问题是:Microsoft 以这种方式实现 WCF 是否有任何充分的理由?是否存在 SOAP 不应该使用 ISO-8859-1 的技术原因?对我来说,这似乎违反了 Postel 定律,拒绝了几乎任何 XML 解析器都可以处理的消息。
We have some issues with a WCF SOAP service that we publish, and is consumed by one of our customers. The issue is that the customer makes SOAP requests encoded as ISO-8859-1, which results in the error message:
HTTP/1.1 415 Cannot process the message because the content type 'text/xml;charset=iso-8859-1' was not the expected type 'text/xml; charset=utf-8'.
Apparently, WCF only supports UTF-8 and UTF-16 big endian, according to http://msdn.microsoft.com/en-us/library/ms751486(v=VS.90).aspx. The problem is also discussed in these questions on SO:
- Calling a webservice that uses ISO-8859-1 encoding from WCF
- WCF Exception: Text Message Encoding and ISO-8859-1 Encoding
While I agree that unicode is preferred, I'm not in a position to force our customer to change their client, so I will have to fix our service.
It bugs me that I need to put >150 lines of code written by some MSDN author into my project - I can't maintain that code or take responsibility for it. I've also spent quite some time to Google for solutions and try to understand them, which I feel is a waste of time for something that could be handled out of the box by WCF.
My question is: is there any good reason for Microsoft to implement WCF this way? Is there a technical reason why SOAP shouldn't use ISO-8859-1? To me, this seems to break Postel's law, rejecting messages that can really be handled by almost any XML parser out there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
显然,这是 SOAP 1.2 规范允许的,请参阅第 4.2 节:
在这种情况下,WCF 选择仅支持 UTF-8 和 UTF-16,而不支持其他编码。
Apparently, this is allowed in the SOAP 1.2 specification, see section 4.2:
In this case, WCF chooses to support only UTF-8 and UTF-16, not other encodings.
一种解决方法是使用 HttpWebRequest 类以“旧时代”方式使用服务,如此处。您将必须处理纯 Xml 来发出请求和其他很酷的事情,但工作得很好!
One workaround is consume the service in an "old times" way using the HttpWebRequest class, as described here. You will have to deal with pure Xml to make requests and other cool things, but works fine!