Skip to content

Add custom charset support

Nicholas Sizer edited this page Nov 7, 2018 · 3 revisions

⚠️ NOTE: Not available in versions prior to 0.13.0

Introduction

ASE allows for a multitude of "charsets" (string encodings) to be used over-the-wire between the driver and the server.

Out of the box, .net core (and therefore the driver) does not support very many of these -- only the most common ones, such as:

  • ASCII
  • UTF-8
  • ISO-8859-1

For sure, there are legacy ASE systems which use different charsets to the above, so the driver has a facility to enable support...

How to add support for a charset

You (the driver user) can add support for a charset by extending System.Text.EncodingProvider and registering the extension via System.Text.Encoding.RegisterProvider(...).

When the server informs the driver that a particular charset should be used, the driver will attempt to load that encoding by name, using Encoding.GetEncoding(string name).

In addition, for many of the charsets ASE supports, you can leverage existing Encoding implementations provided by the System.Text.CodePagesEncodingProvider which is part of the System.Text.Encoding.CodePages NuGet package.

Below is a more concrete example of how to accomplish this.

Example: add support for the cp850 charset

  1. Add the System.Text.Encoding.CodePages NuGet package
  2. Extend EncodingProvider to map "cp850" to an Encoding
public class MyEncodingProvider : EncodingProvider
{
    public override Encoding GetEncoding(int codepage)
    {
        return null; // we're only matching on name, not codepage
    }

    public override Encoding GetEncoding(string name)
    {
        if(string.Equals("cp850", name, StringComparison.OrdinalIgnoreCase))
        {
            return Encoding.GetEncoding(850); // this will load an encoding from the CodePagesEncodingProvider
        }
        return null;
    }
}
  1. Register CodePagesEncodingProvider and MyEncodingProvider
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Encoding.RegisterProvider(new MyEncodingProvider());
  1. Start using the driver