Custom Message Encoding

Damir Dobric Posts

Next talks:

 

Damir@Phone    

Follow me on Twitter: #ddobric



 

 

Archives

When connecting to a web service sometimes you may get following exception or similar:

"Content Type text/xml; charset=utf-8 was not supported by service http://localhost:8000/calculator.  The client and service bindings may be mismatched."

Problem Description

To avoid this problem, usually it seem to be enough to change the property 'textMessageEncoding' of for example BasicHttpBinding. Unfortunately, this property specifies the kind of message-encoding and not the kind of text-encoding. It menas it can take valueslike  MTOM, Text, Binary or even Json.

To specify the right text encoding you will have to define a custom binding like shown at picture below:

image

This would be helpful as long the required encoding is one of WCF-supported recordings. Following code snippet shows
how WCF internally defines supported recordings:

static TextEncoderDefaults()
{
    Encoding = Encoding.GetEncoding("utf-8");
    SupportedEncodings = new Encoding[] { Encoding.UTF8, Encoding.Unicode, Encoding.BigEndianUnicode };
    CharSetEncodings = new CharSetEncoding[]
     {
           new CharSetEncoding("utf-8", Encoding.UTF8),
           new CharSetEncoding("utf-16LE", Encoding.Unicode),
          new CharSetEncoding("utf-16BE", Encoding.BigEndianUnicode),
          new CharSetEncoding("utf-16", null),
          new CharSetEncoding(null, null) };
  }

As you see there are just few supported encoding. In other words, if you have a service which requires ISO-8859-1, this will not work.
To solve this problem I wrote the custom encoder which supports many other encoding. Here are some examples:

 

EUCCN, EUCJP
EUCKR
GB18030
ISCIIAssemese
ISCIIBengali
ISCIIDevanagari
ISCIIGujarathi
ISCIIKannada
ISCIIMalayalam
ISCIIOriya
ISCIIPanjabi
ISCIITamil
ISCIITelugu
ISO_8859_1
ISO_8859_8_Visual
ISO_8859_8I
ISO2022JP
ISO2022JPESC
ISO2022JPSISO
ISOKorean
ISOSimplifiedCN
latin1Encoding

Solution

1. Go to following link an download the DLL which supports all encoding listed above.
2. Add reference to downloaded assembly Daenet.MessageEncoder.Dll.
3. Extend client's configuration file as shown below:

 

image


Posted Apr 18 2008, 11:52 PM by Damir Dobric

Comments

Hassan Gulzar wrote re: Custom Message Encoding
on 07-13-2010 17:59

I added the reference to the DLL but I'm unable to use the extension. I get this error:

The element 'binding' has invalid child element 'daenetTextMessageEncoding'. List of possible elements expected: 'context, binaryMessageEncoding, compositeDuplex, oneWay, httpTransport, httpsTransport, msmqIntegration, msmqTransport, mtomMessageEncoding, namedPipeTransport, peerTransport, pnrpPeerResolver, privacyNoticeAt, reliableSession, security, sslStreamSecurity, tcpTransport, textMessageEncoding, windowsStreamSecurity, transactionFlow, unrecognizedPolicyAssertions, useManagedPresentation, webMessageEncoding, discoveryClient, byteStreamMessageEncoding'

developers.de is a .Net Community Blog powered by daenet GmbH.