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

I am trying to call a webservice using WCF that uses the following encoding:

<?xml version="1.0" encoding="ISO-8859-1" ?> 

I cannot change the encoding for this webservice. I have generated a wcf proxy and when I try and call the proxy, I get the following error:

FailedSystem.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.

Has anyone any idea how I call a webservice that is not UTF-8 using wcf?


Solution 1:

You cannot do this out of the box in WCF - unfortunately.

I faced the same issue, and came up with a custom binding (either in config or in code) that you can use. It's based on HTTP transport, and if that's of help to you, I could post the custom config here, or send you a link to the C# code for the binding.

This MSDN page here 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.

This blog post here shows some additional things to take into account when trying to get that custom text encoder to work properly.

Hope that helps.

Marc

UPDATE:
Based on the Microsoft sample I mentioned above for a CustomTextEncoder, you can easily create a custom binding with a ISO-8859-1 text message encoding - just use this snippet of config (assuming you have downloaded and compiled and referenced that Microsoft sample):

  <system.serviceModel>
    <extensions>
      <bindingElementExtensions>
        <add name="customTextMessageEncoding"
             type="Microsoft.ServiceModel.Samples.CustomTextMessageEncodingElement, 
                   Microsoft.ServiceModel.Samples.CustomTextEncoder"/>
      </bindingElementExtensions>
    </extensions>
    <bindings>
      <customBinding>
        <binding name="ISO8859Binding" >
          <customTextMessageEncoding encoding="ISO-8859-1" />
          <httpTransport />
        </binding>
      </customBinding>
    </bindings>

    <client>
      <endpoint name="Test"
                address="......."
                binding="customBinding"
                bindingConfiguration="ISO8859Binding" 
                contract="IYourContract" />
    </client>
  </system.serviceModel>

You can find that code, plus a ISO88591Binding that basically wraps this whole config into code, on my Skydrive directory WCF ISO 8859-1 Encoding. Watch out, though - this is based on my requirements I had at the time, e.g. my service I needed to talk to required https, and also some other a bit whacky settings, so you might need to tweak those or make them configurable in config, if needed. That ISO88591Binding" project also contains a netHttpBinding that again is a Microsoft-provided sample which I used to get the hang of writing my own custom binding in code.

Writing a custom binding in WCF is definitely possible - but not really for the faint of heart....

Solution 2:

Another option is using the older .NET Framework 2.0 ASMX WebServices technology which supports iso-8859-1 out of the box:

enter image description here

enter image description here

And if the service uses Basic HTTP Authentication you can specify it like this:

TheService theService = new TheService();
theService.Credentials = new NetworkCredential("username", "password");

Solution 3:

In this link you can download the files for make the custom binding, and do the following in your code:

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

myWebService client = new myWebService();

client.Endpoint.Binding = binding;

Solution 4:

If you don't want to deal with tons of downloaded codes and low level implementation you can workaround by using a old style request using HttpWebRequest class, as described here. Now, you will exempt of automatized code and facilities of the Interface and will play with manual Xml parsing.