为什么WCF默认不支持UTF-8和UTF-16以外的其他编码?

发布于 2024-12-28 04:30:31 字数 1103 浏览 0 评论 0原文

我们发布并由我们的一位客户使用的 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:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里的微风 2025-01-04 04:30:31

显然,这是 SOAP 1.2 规范允许的,请参阅第 4.2 节:

如果使用 XML 信息集的 XML 序列化,则绑定可以强制执行
使用特定的字符编码或编码集。

在这种情况下,WCF 选择仅支持 UTF-8 和 UTF-16,而不支持其他编码。

Apparently, this is allowed in the SOAP 1.2 specification, see section 4.2:

A binding, if using an XML serialization of the XML infoset, MAY mandate
that a particular character encoding or set of encodings be used.

In this case, WCF chooses to support only UTF-8 and UTF-16, not other encodings.

云醉月微眠 2025-01-04 04:30:31

一种解决方法是使用 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文