1

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
f0rt
  • 1,801
  • 3
  • 17
  • 30
  • They don't. People would have noticed if they did, this is a *very* porpular method. Why do you assume the application hangs there? Did you try debugging? Did you *pause* the application during debugging and check where the problem is? What does the rest of your code do? – Panagiotis Kanavos May 30 '18 at 14:53
  • 1
    Do you mean that they *throw* perhaps, and your application doesn't handle that properly? Do you have a `catch {}` somewhere? – Panagiotis Kanavos May 30 '18 at 14:56
  • 1
    https://stackoverflow.com/questions/37870084/net-core-doesnt-know-about-windows-1252-how-to-fix – Hans Passant May 30 '18 at 14:59
  • Does this answer your question? [System.NotSupportedException: No data is available for encoding 1252](https://stackoverflow.com/questions/50858209/system-notsupportedexception-no-data-is-available-for-encoding-1252) – Mehdi Dehghani Oct 12 '20 at 18:16

1 Answers1

6

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.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236