Missing Encoding in .Net Core Apps

Georg Dangl by Georg Dangl in DotNet Monday, September 26, 2016

Monday, September 26, 2016

Posted in DotNet

So, today, while migrating a project to .Net Core, I've encountered a failing unit test that told me this:

Missing Encoding Unit Test Error in .Net Core

Code page 1252 ist the Western European standard code page, but I was obviously missing it. That's where I found svicks post on StackOverflow, which basically explained what went wrong:

In .Net Core, available encodings need to be registered before you can access them.

First, you need to import the System.Text.Encoding.CodePages​ dependency and define a compilation value (I chose NETCORE) in your project.json:

"frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dnxcore50",
        "portable-net45+win8"
      ],
      "dependencies": {
        "Microsoft.NETCore.App": {
          "version": "1.0.1",
          "type": "platform"
        },
        "System.Text.Encoding.CodePages": "4.0.1"
      },
      "buildOptions": {
        "define": [ "NETCORE" ]
      }
    },
    "net461": {}
  }

Next, register the provider from within your code via conditional compilation:

#if NETCORE
                    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
#endif

Happy Encoding=)

 


Share this post


comments powered by Disqus

About me

Hi, my name's George! I love coding and blogging about it. I focus on all things around .Net, Web Development and DevOps.

DanglIT

Need a consultant for BIM, GAEB or Software Development?

Contact me at [email protected], +49 (173) 56 45 689 or visit my professional page!

Dangl.Blog();
// Just 💗 Coding

Social Links