1

In .NET Core 2.0 on Windows IIS, I am reading a partial html file in to a string:

   string sWelcomeContent = System.IO.File.ReadAllText(welcomepagePath, System.Text.Encoding.GetEncoding(1252));

This works normally, but very rarely (it happened twice last year), I get an error: System.NotSupportedException: No data is available for encoding 1252.

Any ideas why this happens?

Pieter van Kampen
  • 1,957
  • 17
  • 21

1 Answers1

6

Add System.Text.Encoding.CodePages NuGet package to solution and use the following piece of code in application startup class to registester new encodings.

public void ConfigureServices(IServiceCollection services)
{
    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

    // more code here
}
G43beli
  • 3,835
  • 4
  • 21
  • 28
  • http://gunnarpeipman.com/2017/10/no-data-is-available-for-encoding/ **or** https://stackoverflow.com/questions/49215791/vs-code-c-sharp-system-notsupportedexception-no-data-is-available-for-encodin – G43beli May 11 '18 at 11:20
  • 1
    Not sure this addresses the issue of the OP. OP clearly states that the above code is working in general just in rare occasions it fails – Tseng May 11 '18 at 12:02