AngleSharp: Missing System.Text.Encoding.CodePages reference

Bug Report

Description

Missing reference to System.Text.Encoding.CodePages in latest (0.10.0) version

Steps to Reproduce

  1. Reference AngleSharp NuGet package in your project
  2. Use AngleSharp
class Program
    {
        static void Main(string[] args)
        {
            var parser = new HtmlParser();
            var result = parser.ParseDocument("<html></html>");
        }
    }
  1. Result: Exception
Unhandled Exception: System.TypeInitializationException: The type initializer for 'AngleSharp.Configuration' threw an exception. ---> System.IO.FileNotFoundException: Could not lo
ad file or assembly 'System.Text.Encoding.CodePages, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

Expected behavior: AngleSharp does not throw initialization exception

Actual behavior: Exception thrown.

Environment details: .netcore console app

Possible Solution

Manually adding reference to mentioned package fixes the issue

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 29 (13 by maintainers)

Most upvoted comments

@FlorianRappl I was trying to piggy-back on this issue to avoid opening a new.

W10, VS17, .NET Framework 4.7.2 project

AngleSharp 0.11.0.0 installed via NuGet Package Manager

Yes it installs dependency System.Text.Encoding.CodePages v4.5.0 but this does not resolve issue :

class Program
    {
        static void Main(string[] args)
        {
            var parser = new HtmlParser();
            var result = parser.ParseDocument("<html></html>");
        }
    }

Could not load file or assembly ‘System.Text.Encoding.CodePages, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ or one of its dependencies. The system cannot find the file specified.

If I manually update to System.Text.Encoding.CodePages v4.5.1 it does not help.

I think we’ve confirmed this is definitely not an AngleSharp issue. For anyone with the sae issue, what we’ve found is the binding redirect for System.Text.Encoding.CodePages is being ignored.

In environments that work fusion logs clearly show the binding redirect being picked up:

LOG: Redirect found in application configuration file: 4.1.1.0 redirected to 4.1.3.0. LOG: Post-policy reference: System.Text.Encoding.CodePages, Version=4.1.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

In the one environment where the error occurs, these lines are missing from the fusion logs.

We don’t have a solution yet as other binding redirects are working from the applications configuration as expected.

I’ll put this question elsewhere now to try and resolve.

Worked out the issue, issue was because it was in class library and the main project needed to have the bindings also.

I had the same problem and in my case this was the setup and solution:

Setup

In my solution I have: ConsoleProgram which depends on MyLibrary which depends on AngleSharp.

All are .NET 4.7.2 projects using the old .csproj format, not the new SDK style format. Note that ConsoleProgram doesn’t depend on AngleSharp directly.

I have removed all manual binding redirects from my app.config files and ensured that Auto-generate binding redirects was enabled for all my projects in the solution. Read more about how automatic binding redirects work. The key here is that the original app.config files have no binding redirects, but the generated .exe.config and .dll.config files in our output directory do have binding redirects.

After inspection of the generated .config files, I noticed that the System.Text.Encoding.CodePages binding redirect that I expected wasn’t generated. This explained why I was receiving a FileNotFoundException.

Solution

The solution is to add a reference to AngleSharp or System.Text.Encoding.CodePages in my ConsoleProgram. It seems the automatic binding redirect generation works only for the top-level project. It doesn’t traverse nested projects. By making ConsoleProgram depend directly on the required .dll files, the generated ConsoleProgram.exe.config then contains the necessary binding redirects (auto-generated) and hence solved my problem.

Hi @FlorianRappl,

Thanks for getting back to me. It may not be any problem with AngleSharp, but we’ve spent a long time trying to ascertain the differences and reasons for the error. The only call with this error is the AngleSharp usage and also mentioned in the fusion logs, so I was hopeful someone had the same error and found a solution.

I did look at #842 however we are already using the 0.14 version. We’ve also had this deployed for some time on various servers without issue. The application is asp.net targeting .NET 4.7.2 definitely (both at build time and at runtime in configuration).

The only difference I can see is the server with the issue is 2012 R2 server, not sure if there is some difference there. 4.7.2 framework is installed (verified through registry and had it reinstalled for safe measure).

I am building a standalone console application and working through some diagnostics to see if we can get any further detail on why this is happening.

If I find a solution I’ll post it.

I’m seeing the same issue now, this is happening on one deployment but it is fine on all other locations including development machines. All have .NET 4.7.2 installed.

I know there must be a logical reason, however I can’t figure it out.

I don’t want to file a new bug report as it seems the same issue.

LOG: DisplayName = System.Text.Encoding.CodePages, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a (Fully-specified) LOG: Appbase = *** LOG: Initial PrivatePath = *** Calling assembly : AngleSharp, Version=0.14.0.0, Culture=neutral, PublicKeyToken=e83494dcdc6d31ea. … WRN: Comparing the assembly name resulted in the mismatch: Build Number ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

Adding the nuget package to AngleSharp results in the following binding redirect. This is definitely at the deployed location that has the issue.

     <dependentAssembly>
        <assemblyIdentity name="System.Text.Encoding.CodePages" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.1.3.0" newVersion="4.1.3.0" />
     </dependentAssembly>

The exact same file version of System.Text.Encoding.CodePages.dll is in the bin folder as well.

Is anyone still experiencing this issue or has resolved it with the latest version after experiencing it?

I faced same issue, and resolved it by adding the binding.

reference to the package and the binding, both are required in consuming library and the final executing library.

point to note though: earlier this was working without adding the binding - problem came after i updated the package.

No problem thanks for the info @havremunken - glad you figured it out! 🍻

If anything else pops up feel free to create another issue or message me directly. We also have a Gitter chat for more dynamic interactions.

Small update here; No AngleSharp, System.Text.Encoding.CodePages or System.Runtime.CompilerServices.Unsafe in the GAC on the server.