locked
How to make UTF-16 as default encoding for WCF Data Services. RRS feed

  • Question

  • Hi

    I am writing a WCF data services to expose data that contains languages such as Japanese and Chinese

    By default I am getting the response in UTF8 and as a result I am not able to correctly display the data of theses languages    

    I have already gone through similar threads in forum tried tags mentioned below but no luck.

      <binding writeEncoding="utf-16">

    <globalization fileEncoding="utf-16" responseEncoding="utf-16" responseHeaderEncoding="utf-16"  />

    The Provider is amusing is Devart as data provider  

    It will be great help if someone can give me a pointer for enabling UTF16 in data services

    Thanks In advance

    Regards

    Ashwini


    Friday, February 17, 2012 3:16 PM

Answers

  • Hi,

    As noted above, UTF-8 supports the exact same set of characters as UTF-16. So unless you have a client which can only consume UTF-16 and not UTF-8 there should be no reason to use UTF-16 over UTF-8. To the contrary in fact, UTF-16 payloads are almost 2 times larger than UTF-8.

    There's no "simple" way to make it default. One way would be to modify the headers on the server before the request gets processed by WCF Data Services. You could use this JSONP sample : http://archive.msdn.microsoft.com/DataServicesJSONP as a starting point. It does much more than you need, so remove most of its code, but it does have the right hooks and also shows how to set the Accept-Charset header.

    Thanks,


    Vitek Karas [MSFT]


    Tuesday, February 21, 2012 2:10 PM
    Moderator

All replies

  • Hi,

    The client can send Accept-Charset: utf-16 header in the request and the server will respond using UTF-16.

    It would also be interesting to know why UTF-8 doesn't work for you. Both UTF-8 and UTF-16 can encode the same set of characters (the whole Unicode set), they just use different way to translate the character into a series of bytes. But any client which knows how to handle UTF-8 should be able to deal with that.

    Thanks,


    Vitek Karas [MSFT]

    Saturday, February 18, 2012 5:59 PM
    Moderator
  • 

    Actually I need to make it default so I do not need to configure all the clients , Also does UTF8 supports chineses and  japanese char ?

    Tuesday, February 21, 2012 1:33 PM
  • Hi,

    As noted above, UTF-8 supports the exact same set of characters as UTF-16. So unless you have a client which can only consume UTF-16 and not UTF-8 there should be no reason to use UTF-16 over UTF-8. To the contrary in fact, UTF-16 payloads are almost 2 times larger than UTF-8.

    There's no "simple" way to make it default. One way would be to modify the headers on the server before the request gets processed by WCF Data Services. You could use this JSONP sample : http://archive.msdn.microsoft.com/DataServicesJSONP as a starting point. It does much more than you need, so remove most of its code, but it does have the right hooks and also shows how to set the Accept-Charset header.

    Thanks,


    Vitek Karas [MSFT]


    Tuesday, February 21, 2012 2:10 PM
    Moderator
  • I did some further experimentation and it seem that we should use character set negotiation by passing appropriate header in client request , 

    Friday, February 24, 2012 1:10 PM