WCF Exception: Text Message Encoding and ISO-8859-1 Encoding

31,692

Does this existing answer help? Calling a webservice that uses ISO-8859-1 encoding from WCF

Share:
31,692
Kiquenet
Author by

Kiquenet

Should "Hi", "Thanks" and taglines and salutations be removed from posts? http://meta.stackexchange.com/questions/2950/should-hi-thanks-and-taglines-and-salutations-be-removed-from-posts What have you tried? http://meta.stackexchange.com/questions/122986/is-it-ok-to-leave-what-have-you-tried-comments Asking http://stackoverflow.com/help/asking Answer http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers http://www.enriquepradosvaliente.com http://kiquenet.wordpress.com ◣◥◢◤◢◤◣◥◢◤◢◤◣◥◢◤ ◥◢◤◢◤◣◥◢◤◢◤◣◥◢◤◢ .NET developer and fan of continuous self-improvement and good patterns and practices. Stuff I am interested in: .NET technology stack in general, C#, Powershell and Javascript in particular as languages Test driven development, DI, IoC and mocking frameworks Data access with ORMs and SQL ASP.NET javascript, jQuery and related frontend frameworks Open source projects

Updated on December 13, 2020

Comments

  • Kiquenet
    Kiquenet over 3 years

    Possible Duplicate:
    Calling a webservice that uses ISO-8859-1 encoding from WCF

    I am trying to consume an external Web Service (the web service has PHP implementation) using VS 2008, .net 3.5, WCF( Environment : Windows XP and VS 2008). I add Service Reference to Web Service, VS generates WCF Proxy.

    Binding is basicHttpBinding.

    I call to method in Web Service, using Proxy, then I started getting a ProtocolException, I received the following error message :

    System.ServiceModel.ProtocolException: The content type text/xml; charset=ISO-8859-1 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 644 bytes of the response were:

    This was successful

    Well, I needed to call a service in iso-8859-1 encoding.

    Any useful sample source code for solve it?

    Update:

    The default encoder used in WCF only works with UTF-8 and UTF-16 (big and little endian).

    If I use textEncoding="iso-8859-1" in binding in app.config,

    I get this error:

    System.ArgumentException: No se admite la codificación de texto 'iso-8859-1' usada en el formato de mensaje de texto. Nombre del parámetro: encoding.

    System.ServiceModel.Channels.TextEncoderDefaults.ValidateEncoding(Encoding encoding) System.ServiceModel.Channels.TextMessageEncodingBindingElement.set_WriteEncoding(Encoding value) System.ServiceModel.BasicHttpBinding.set_TextEncoding(Encoding value) System.ServiceModel.Configuration.BasicHttpBindingElement.OnApplyConfiguration(Binding binding) System.ServiceModel.Configuration.StandardBindingElement.ApplyConfiguration(Binding binding) System.ServiceModel.Description.ConfigLoader.LookupBinding(String bindingSectionName, String configurationName, ContextInformation context) System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(ServiceEndpoint serviceEndpoint, String configurationName) System.ServiceModel.ChannelFactory.ApplyConfiguration(String configurationName) System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName, EndpointAddress address) ctor(String endpointConfigurationName, EndpointAddress remoteAddress) CreateSimplexFactory() CreateChannelFactory() CreateChannelFactoryRef(EndpointTrait`1 endpointTrait) InitializeChannelFactoryRef() ctor() IntegracionEasyVista.ServiceEasyVista.WebServicePortTypeClient..ctor() in

    Update:

    Calling a web service that uses ISO-8859-1 encoding from WCF

    Calling a webservice that uses ISO-8859-1 encoding from WCF

    This MSDN page (http://msdn.microsoft.com/en-us/library/ms751486(v=VS.90).aspx) shows how to create a "CustomTextEncoder" which can support more than utf-8, utf-16 and unicode encodings. It includes full sample source code and was very useful for me to get things going.

    I use CustomTextMessageEncodingElement, but I get error:

    The content type text/xml; charset=ISO-8859-1 of the response message does not match the content type of the binding (text/xml;charset=iso-8859-1). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 1024 bytes of the response were:**

    From code of Samples MSDN, I modify the constructor from CustomTextMessageEncoder class:

    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);
    }
    

    I replace "{0};charset={1}" by "{0}; charset={1}" (I have included an blank)

    Then, I get the error:

    The message version of the outgoing message (Soap11 (http://schemas.xmlsoap.org/soap/envelope/) AddressingNone
    (http://schemas.microsoft.com/ws/2005/05/addressing/none)) does not match that of the encoder

    (Soap12 (http://www.w3.org/2003/05/soap-envelope) Addressing10 (http://www.w3.org/2005/08/addressing)).
    Make sure the binding is configured with the same version as the message.

  • Kiquenet
    Kiquenet over 13 years
    Fails for me, in TextEncoderDefaults.ValidateEncoding(Encoding encoding). System.ArgumentException: Not supports textencoding 'iso-8859-1'
  • tomasr
    tomasr over 13 years
    You're right; I hadn't tested it before. Likely some stupid limitation in WCF.
  • Kiquenet
    Kiquenet over 10 years
    Maybe better delete this answer.
  • stephen ebichondo
    stephen ebichondo over 5 years
    Changing the implementation of CustomTextMessageBindingElement constructor sorted it for me public CustomTextMessageBindingElement() : this("ISO-8859-1") { }
  • AriesConnolly
    AriesConnolly almost 4 years
    I using UTF-8 and I still get this error. I searched my code for 8859 and VS does not find it. Yet another misleading error message!