locked
WCF Exception: Text Message Encoding and ISO-8859-1 Encoding RRS feed

  • Question

  • Hi, 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: 

        <?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://xml.quoteexchange.co.uk/soap/QuoteExchange"><SOAP-ENV:Body><ns1:LeadNotificationResponse xmlns:ns1="http://xml.quoteexchange.co.uk/soap/QuoteExchange"><QXResponse xsi:type="tns:QXLeadNotificationResponse"><Response xsi:type="xsd:string">This was successful</Response></QXResponse></ns1:LeadNotificationResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
    



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



    Any useful sample source code for solve it ?


    edit: 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()



    thanks in advanced

    Should "Hi", "Thanks" and taglines and salutations be removed from posts? http://meta.stackoverflow.com/questions/2950/should-hi-thanks-and-taglines-and-salutations-be-removed-from-posts
    Monday, November 8, 2010 12:00 PM

Answers

  • You don't need to create a class for Soap11; what you need to do is, when you're creating your custom encoder, to use that message version (instead of the default one).
    Friday, November 12, 2010 5:53 PM

All replies

  • Hello,

    The default encoder used in WCF only works with UTF-8 and UTF-16 (big and little endian). If you want to support other encodings (such as iso-8859-1), you can use the custom text encoder listed in the sample at http://msdn.microsoft.com/en-us/library/ms751486.aspx.

    Monday, November 8, 2010 2:42 PM
  • Thanks, I use your link and here

    Calling a webservice that uses ISO-8859-1 encoding from WCF
    http://stackoverflow.com/questions/1908030/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)
                {
                    this.factory = factory;
                    
                    this.writerSettings = new XmlWriterSettings();
                    this.writerSettings.Encoding = Encoding.GetEncoding(factory.CharSet);
                    this.contentType = string.Format("{0};charset={1}", 
                        this.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 <br/>(http://schemas.microsoft.com/ws/2005/05/addressing/none)) does not match that of the encoder <br/><br/>(Soap12 (http://www.w3.org/2003/05/soap-envelope) Addressing10 (http://www.w3.org/2005/08/addressing)).<br/> Make sure the binding is configured with the same version as the message.**



    Should "Hi", "Thanks" and taglines and salutations be removed from posts? http://meta.stackoverflow.com/questions/2950/should-hi-thanks-and-taglines-and-salutations-be-removed-from-posts
    Wednesday, November 10, 2010 11:32 AM
  • Try passing the message version the error message is saying the version of the outgoing message is to the CustomTextMessageBindingElement constructor:

    new CustomTextMessageBindingElement("iso-8859-1", "text/xml", MessageVersion.Soap11);

    Wednesday, November 10, 2010 6:02 PM
  • MessageVersion.Soap11 is a enum ?? Where is the enum ?? Thx
    Should "Hi", "Thanks" and taglines and salutations be removed from posts? http://meta.stackoverflow.com/questions/2950/should-hi-thanks-and-taglines-and-salutations-be-removed-from-posts
    Friday, November 12, 2010 10:16 AM
  • It's not an enum. MessageVersion is a class defined in the System.ServiceModel.Channels namespace. It behaves like an enum, because its constructor is private and you can only use instances from its static properties.
    Friday, November 12, 2010 3:00 PM
  • How can I create class for Soap 11 ? Thx
    Should "Hi", "Thanks" and taglines and salutations be removed from posts? http://meta.stackoverflow.com/questions/2950/should-hi-thanks-and-taglines-and-salutations-be-removed-from-posts
    Friday, November 12, 2010 3:53 PM
  • You don't need to create a class for Soap11; what you need to do is, when you're creating your custom encoder, to use that message version (instead of the default one).
    Friday, November 12, 2010 5:53 PM