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

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

Leave a Comment