内容类型 text/xml;字符集=“utf-8"响应消息的内容类型与绑定的内容类型不匹配 (text/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)

查看:50
本文介绍了内容类型 text/xml;字符集=“utf-8"响应消息的内容类型与绑定的内容类型不匹配 (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;
}

并从同一个地方窃取了 CustomTextMessageBindingElementCustomTextMessageEncoderFactory.

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

Quite a lot of work for two misplaced quotes :D

这篇关于内容类型 text/xml;字符集=“utf-8"响应消息的内容类型与绑定的内容类型不匹配 (text/xml; charset=utf-8)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆