内容类型为text/xml; charset ="utf-8";的响应消息与绑定的内容类型不匹配(文本/xml; charset = utf-8) [英] The content type text/xml; charset="utf-8" of the response message does not match the content type of the binding (text/xml; charset=utf-8)
问题描述
请注意,此问题与WCF Connected Services的.NET Core实现有关.
Please note that this question pertains to the .NET Core implementation of WCF Connected Services.
我正在将常规.NET WCF客户端移植到.NET Core,但是遇到了这个问题:
I am porting a regular .NET WCF client over to .NET Core, but I ran into this issue:
The content type text/xml; charset="utf-8" of the response message does
not match the content type of the binding (text/xml; charset=utf-8).
If using a custom encoder, be sure that the IsContentTypeSupported method is
implemented properly. The first 1024 bytes of the response were:
'<?xml version='1.0' encoding='UTF-8'?> [...]
响应确实包含引号:
HTTP/1.1 200 Ok
content-type: text/xml; charset="utf-8"
在WCF中,我从未做过任何特别的事情来处理这个问题.这是.NET Core版本中的错误,还是真的只是特定于内容类型(utf-8与"utf-8")?
I never did anything special to handle this in WCF proper. Is this a bug in the .NET Core version, or is it just really specific about the content type (utf-8 vs "utf-8")?
如何更改期望的内容类型以匹配正在调用的服务? (我对此无能为力,但是可以根据需要复制和更改WSDL).
How can I change the expected content type to match the service I'm calling? (I have no control over that, but I can copy and alter the WSDL if needed).
我正在使用svcutil生成的客户端. (连接的服务)
I'm using a svcutil-generated client. (Connected Service)
推荐答案
.NET Core版本似乎确实对此更挑剔.无论如何,我设法使用自定义编码器解决了这个问题.
It would indeed seem that the .NET Core version is more picky about this. In any case, I managed to solve it using a Custom Encoder.
我从 Github .我添加了以下方法:
I blatently stole the CustomTextMessageEncoder from Github. I added the following method:
public override bool IsContentTypeSupported(string contentType)
{
return true;
}
然后从同一地方偷走CustomTextMessageBindingElement
和CustomTextMessageEncoderFactory
.
And stole CustomTextMessageBindingElement
and CustomTextMessageEncoderFactory
from the same place.
我通过创建一个自定义绑定添加了它们(basicBinding是我以前拥有的绑定):
I added them by creating a custom binding (basicBinding is the binding I had before):
var customBindingElement = new CustomTextMessageBindingElement("UTF-8", "text/xml", MessageVersion.Soap11);
var binding = new CustomBinding(basicBinding);
binding.Elements.RemoveAt(0);
binding.Elements.Insert(0, customBindingElement);
var client = (T2)Activator.CreateInstance(typeof(T), binding, address);
我使用Activator来动态生成代理.只需替换为对WCF生成的客户端的调用即可.
I use Activator as I generate my proxies dynamically. Just replace with a call to the WCF generated client.
对于两个错误的引号,D进行了大量工作:D
Quite a lot of work for two misplaced quotes :D
这篇关于内容类型为text/xml; charset ="utf-8";的响应消息与绑定的内容类型不匹配(文本/xml; charset = utf-8)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!