[英]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)
Please note that this question pertains to the .NET Core implementation of WCF Connected Services.请注意,此问题与 WCF 连接服务的 .NET Core 实现有关。
I am porting a regular .NET WCF client over to .NET Core, but I ran into this issue:我正在将一个普通的 .NET WCF 客户端移植到 .NET Core,但我遇到了这个问题:
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'?> [...]
The response indeed contains the quotes:响应确实包含引号:
HTTP/1.1 200 Ok
content-type: text/xml; charset="utf-8"
I never did anything special to handle this in WCF proper.我从来没有做任何特别的事情来在 WCF 中正确处理这个问题。 Is this a bug in the .NET Core version, or is it just really specific about the content type (utf-8 vs "utf-8")?这是 .NET Core 版本中的错误,还是仅针对内容类型(utf-8 与“utf-8”)进行了具体说明?
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). (我无法控制它,但如果需要,我可以复制和更改 WSDL)。
I'm using a svcutil-generated client.我正在使用 svcutil 生成的客户端。 (Connected Service) (连接服务)
It would indeed seem that the .NET Core version is more picky about this. .NET Core 版本似乎确实对此更加挑剔。 In any case, I managed to solve it using a Custom Encoder.无论如何,我设法使用自定义编码器解决了它。
I blatently stole the CustomTextMessageEncoder from Github .我公然从Github窃取了 CustomTextMessageEncoder。 I added the following method:我添加了以下方法:
public override bool IsContentTypeSupported(string contentType)
{
return true;
}
And stole CustomTextMessageBindingElement
and CustomTextMessageEncoderFactory
from the same place.并从同一个地方窃取了CustomTextMessageBindingElement
和CustomTextMessageEncoderFactory
。
I added them by creating a custom binding (basicBinding is the binding I had before):我通过创建自定义绑定来添加它们(basicBinding 是我之前的绑定):
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);
I use Activator as I generate my proxies dynamically.我使用 Activator 来动态生成代理。 Just replace with a call to the WCF generated client.只需替换为对 WCF 生成的客户端的调用。
Quite a lot of work for two misplaced quotes :D两个错位的报价需要做很多工作:D
I was getting the exact same error after converting a WCF Client to dotnet core.将 WCF 客户端转换为 dotnet 核心后,我遇到了完全相同的错误。 I have to refactor some.我必须重构一些。 I replaced my binding code to this.我将绑定代码替换为此。 Note WsBinding isn't native in dotnet core.注意 WsBinding 在 dotnet 核心中不是原生的。
https://github.com/dotnet/wcf/issues/1370 https://github.com/dotnet/wcf/issues/1370
I had the same problem also.我也有同样的问题。 There might be an issue with security-related or content-type format differences.可能存在与安全相关或内容类型格式差异的问题。 I got over the problem by ignoring ServiceReference bindings.我通过忽略 ServiceReference 绑定解决了这个问题。 You should directly call with HttpWebRequest As a sample;你应该直接调用HttpWebRequest作为示例;
private string GetWebService(string token, int testId, string phone)
{
try
{
var serviceUrl = "https://test.com/Soap";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
httpWebRequest.ContentType = "text/xml";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
var xmlData = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:doc=\"http://test.com\">" +
" <soapenv:Header/>" +
" <soapenv:Body>" +
" <doc:AskForStackOverFlow>" +
" <doc:token>" + token + "</doc:token>" +
" <doc:testId>" + testId+ "</doc:testId>" +
" <doc:phone>" + phone + "</doc:phone>" +
" <doc:startTime>" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "</doc:startTime>" +
" </doc:AskForStackOverFlow>" +
" </soapenv:Body>" +
"</soapenv:Envelope>";
streamWriter.Write(xmlData);
}
var xmlResult = "";
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
xmlResult = streamReader.ReadToEnd();
}
if (string.IsNullOrEmpty(xmlResult))
return "";
var result = "";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlResult);
XmlNodeList nodes = doc.GetElementsByTagName("ns1:AskForStackOverFlowResult");
foreach (XmlNode item in nodes)
{
result = item.InnerText;
}
return result;
}
catch (Exception ex)
{
return ex.Message;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.