I'm calling the lines below. The calls hang infinitely.
Encoding.GetEncoding(1251);
Encoding.GetEncoding("Windows-1251");
I'm using:
- VS 2017 15.7.2
- dotnet --version > "2.1.201"
- Windows 10
I'm calling the lines below. The calls hang infinitely.
Encoding.GetEncoding(1251);
Encoding.GetEncoding("Windows-1251");
I'm using:
The method doesn't block. Most likely it throws and the rest of the code handles the exception in a way that makes it seem like the code is blocking. Perhaps there's a catch {}
somewhere
From the .NET Core notes in Character Encodings in .NET
By default, .NET Core does not make available any code page encodings other than code page 28591 and the Unicode encodings, such as UTF-8 and UTF-16. However, you can add the code page encodings found in standard Windows apps that target .NET to your app. For complete information, see the CodePagesEncodingProvider topic.
That's not so strange since .NET and Windows use UTF16 already and almost all web sites, REST APIs, files use UTF8.
If you try a simple console application :
static void Main(string[] args)
{
var enc=Encoding.GetEncoding(1251);
Console.WriteLine(enc);
Console.WriteLine("Hello World!");
}
You'll get an exception :
Unhandled Exception: System.NotSupportedException: No data is available for encoding 1251.
For information on defining a custom encoding, see the documentation for the
Encoding.RegisterProvider method.
at System.Text.Encoding.GetEncoding(Int32 codepage)
at encodingtest.Program.Main(String[] args) in H:\Projects\encodingtest\Program.cs:line 11
You can install additional codepages by adding the System.Text.Encoding.CodePages package and registering it :
static void Main(string[] args)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var enc=Encoding.GetEncoding(1251);
Console.WriteLine(enc);
Console.WriteLine("Hello World!");
}
This time the application won't throw.