Encoding.RegisterProvider(CodePagesEncodingProvider.Instance) does not add extra encoding providers

北城余情 提交于 2020-03-19 05:18:17

问题


I am developing a netcoreapp2.0 console application and I need access to the whole encoding package from .NET.

I have already added the System.Text.Encoding.CodePages Version=4.4.0 Nuget package from this page to my project and cleaned/restored the project several time.

However I can't get the extra encoding I need.

The following code:

Console.WriteLine(Encoding.GetEncodings().Length);
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Console.WriteLine(Encoding.GetEncodings().Length);

returns:

8
8

and no extra encoding is added to Encoding class.

These are all the references I have in my project:

<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.0.0" />
<PackageReference Include="System.Data.HashFunction.xxHash" Version="2.0.0-ci-00012" />
<PackageReference Include="Nager.PublicSuffix" Version="1.1.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.4.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" />

<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />

Am I doing something wrong here?

Update

After reinstalling the .NET Core SDK 2.0.2 again, things started to work fine and I can use the extended Encoding code pages using Encoding.GetEncoding() method, however the Encoding.GetEncodings() still returns the initial 8 Encoding code pages.


回答1:


I am adding this as the answer regarding Gabriel's suggestion that it might help other people in the future too.

I cleaned the Nuget cache, repaired my ".NET Core SDK 2.0.2" installation, did a "Solution and Project Cleanup" and rebuilt my project, and the Encoding.RegisterProvider(CodePagesEncodingProvider.Instance) code started to work fine.

Now I can access the extra code pages using the Encoding.GetEncoding() method, however I still get the same list of encodings from Encoding.GetEncodings() method after adding the extra encodings.

From my perspective the Encoding.GetEncodings() method should return all the encodings available after more of them have been added.




回答2:


install nuget packages: System.Text.Encoding & System.Text.Encoding.CodePages



来源:https://stackoverflow.com/questions/46994907/encoding-registerprovidercodepagesencodingprovider-instance-does-not-add-extra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!