我有一个新的 WCF 服务,一些现有客户端需要能够与之通信。
有些客户端错误地发送了带有“text/xml”的 Content-Type header 的 SOAP 请求;字符集=us-ascii'。目前,客户本身无法更改。
当他们发送请求时,他们收到错误消息:
HTTP/1.1 415 Cannot process the message because the content type 'text/xml; charset=us-ascii' was not the expected type 'text/xml; charset=utf-8'
有什么方法可以指示 WCF 服务忽略 Content-Type 的字符集并假定为 utf-8?
最佳答案
这可能对你有帮助,如果没有我会删除答案。我认为这可以在您的上下文中使用,但我不太确定,因为没有其他详细信息。
WebContentTypeMapper 可用于覆盖发送的内容类型。这是 WCF 示例集中使用的代码的片段。在此示例中,它接收发送的任何内容类型并将其映射到 json,但您可以根据需要进行调整。
如果您还没有,可以从 WCF Samples 下载示例。 此示例位于 ..WF_WCF_Samples\WCF\Extensibility\Ajax\WebContentTypeMapper\CS
using System.ServiceModel.Channels;
namespace Microsoft.Samples.WebContentTypeMapper
{
public class JsonContentTypeMapper : System.ServiceModel.Channels.WebContentTypeMapper
{
public override WebContentFormat GetMessageFormatForContentType(string contentType)
{
if (contentType == "text/javascript")
{
return WebContentFormat.Json;
}
else
{
return WebContentFormat.Default;
}
}
}
}
关于WCF SOAP 服务忽略内容类型字符集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44698210/