C# WCF例外:文本消息编码和ISO-8859-1编码

C# WCF例外:文本消息编码和ISO-8859-1编码,c#,wcf,encoding,exception-handling,iso-8859-1,C#,Wcf,Encoding,Exception Handling,Iso 8859 1,可能重复: 我试图使用VS2008、.NET3.5、WCF(环境:WindowsXP和VS2008)使用外部Web服务(Web服务有PHP实现)。我将服务引用添加到Web服务,VS生成WCF代理 绑定是基本的TPBinding 我使用代理调用Web服务中的方法,然后开始获取ProtocolException,收到以下错误消息: System.ServiceModel.ProtocolException: 内容类型为text/xml; 字符集=响应的ISO-8859-1 消息与内容不匹配 绑定的

可能重复:

我试图使用VS2008、.NET3.5、WCF(环境:WindowsXP和VS2008)使用外部Web服务(Web服务有PHP实现)。我将服务引用添加到Web服务,VS生成WCF代理

绑定是基本的TPBinding

我使用代理调用Web服务中的方法,然后开始获取ProtocolException,收到以下错误消息:

System.ServiceModel.ProtocolException: 内容类型为text/xml; 字符集=响应的ISO-8859-1 消息与内容不匹配 绑定的类型(text/xml; 字符集=utf-8)。如果使用自定义 编码器,确保 IsContentTypeSupported方法是 执行得当

响应的前644个字节是:

这是成功的

我需要调用iso-8859-1编码的服务

有没有有用的示例源代码来解决这个问题

更新:

WCF中使用的默认编码器仅适用于UTF-8和UTF-16(大端和小端)

如果我在app.config中的绑定中使用textEncoding=“iso-8859-1”

我得到这个错误:

System.ArgumentException:不允许 “iso-8859-1”文本编码 我们需要一种新的格式 texto。NoBr.Del PalaMetray:编码:

System.ServiceModel.Channel.TextEncoderDefaults.ValidateEncoding(编码 编码) System.ServiceModel.Channel.TextMessageEncodingBindingElement.set_WriteEncoding(编码 价值) System.ServiceModel.BasicHttpBinding.set\u TextEncoding(编码 价值) System.ServiceModel.Configuration.BasicHttpBindingElement.OnApplyConfiguration(绑定 装订) System.ServiceModel.Configuration.StandardBindingElement.ApplyConfiguration(绑定 装订) System.ServiceModel.Description.ConfigLoader.LookupBinding(字符串 bindingSectionName,字符串 配置名称,上下文信息 (上下文) System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(ServiceEndpoint serviceEndpoint,字符串 配置名称) System.ServiceModel.ChannelFactory.ApplyConfiguration(字符串 配置名称) System.ServiceModel.ChannelFactory.InitializeEndpoint(字符串 配置名称、端点地址 地址)ctor(字符串 endpointConfigurationName, 端点地址(远程地址) CreateSimplexFactory() CreateChannelFactory() CreateChannelFactoryRef(EndpointTrait`1 端点(特征) InitializeChannelFactoryRef()构造函数() IntegracionEasyVista.ServiceEasyVista.WebServicePortTypeClient..ctor() 在

更新:

从WCF调用使用ISO-8859-1编码的web服务

此MSDN页面()显示如何创建一个“CustomTextEncoder”,它可以支持多种utf-8、utf-16和unicode编码。它包括完整的示例源代码,对我来说非常有用

我使用CustomTextMessageEncodingElement,但得到错误:

内容类型为text/xml;响应消息的charset=ISO-8859-1与绑定的内容类型不匹配(text/xml;charset=ISO-8859-1)。如果使用自定义编码器,请确保正确实现IsContentTypeSupported方法。响应的前1024个字节是:**

从示例MSDN的代码中,我修改了CustomTextMessageEncoder类的构造函数:

public CustomTextMessageEncoder(CustomTextMessageEncoderFactory factory)
{
    writerSettings = new XmlWriterSettings();
    writerSettings.Encoding = Encoding.GetEncoding(factory.CharSet);
    contentType = string.Format("{0};charset={1}", 
    factory.MediaType, this.writerSettings.Encoding.HeaderName);
}
我将“{0};字符集={1}”替换为“{0};字符集={1}”(我包含了一个空格)

然后,我得到一个错误:

传出消息(Soap11()AddressingNone
())的消息版本与编码器(Soap12()Addressing10())的消息版本不匹配。
请确保使用与消息相同的版本配置绑定


我认为您应该能够通过显式更改绑定配置来解决这个问题,将属性设置为“iso-8859-1”,而不是默认值“utf-8”

这个现有答案有用吗

在TextEncoderDefaults.ValidateEncoding(编码)中,我失败。System.ArgumentException:不支持文本编码“iso-8859-1”您是对的;我以前没试过。WCF中可能存在一些愚蠢的限制。也许最好删除此答案。更改CustomTextMessageBindingElement构造函数的实现为我排序了公共CustomTextMessageBindingElement():this(“ISO-8859-1”){我在使用UTF-8时仍然会遇到这个错误。我在我的代码中搜索了8859,但是VS没有找到它。又一个误导性的错误信息!