question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Azure functions: Could not load file or assembly System.Text.Encoding.CodePages

See original GitHub issue

Bug Report

Prerequisites

  • Can you reproduce the problem in a MWE?
  • Are you running the latest version of AngleSharp?
  • Did you check the FAQs to see if that helps you?
  • Are you reporting to the correct repository? (there are multiple AngleSharp libraries, e.g., AngleSharp.Css for CSS support)
  • Did you perform a search in the issues?

For more information, see the CONTRIBUTING guide.

Description

When using an Azure Functions project, I can’t load the basic configuration out-of-the-box. When reaching the point where the config has to load, it throws the exception being reported.

Steps to Reproduce

  1. [Create an Azure Functions project from scratch (did this with VS2019)]
  2. [Create a function]
  3. [Add the library in the list of dependencies] Example code for function
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using AngleSharp;

namespace anglesharp_functions
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            var config = Configuration.Default;

            //Create a new context for evaluating webpages with the given config
            var context = BrowsingContext.New(config);

            //Parse the document from the content of a response to a virtual request
            var document = await context.OpenAsync(req => req.Content("<h1>Some example source</h1><p>This is a paragraph element"));

            //Do something with document like the following
            Console.WriteLine("Serializing the (original) document:");
            Console.WriteLine(document.DocumentElement.OuterHtml);

            var p = document.CreateElement("p");
            p.TextContent = "This is another paragraph.";

            Console.WriteLine("Inserting another element in the body ...");
            document.Body.AppendChild(p);

            Console.WriteLine("Serializing the document again:");
            Console.WriteLine(document.DocumentElement.OuterHtml);

            string responseMessage = document.DocumentElement.OuterHtml;

            return new OkObjectResult(responseMessage);
        }
    }
}

Expected behavior: [The configuration to load and have an html output at the end of the execution of the function]

Actual behavior: [Throws an exception]

Environment details: [OS, .NET Runtime, …]

  • Windows 10 Pro 1909
  • Azure Functions SDK 3.0.11
  • Visual Studio 2019 16.9.4

What I’ve tried so far:

Because the missing dll was getting purged from the output directory (bin/Debug/netcoreapp3.1/bin) I’ve tried to manually install the library with nuget and use the <FunctionsPreservedDependencies> directive in the *.csproj. So far, the SDK seems to delete it but I don’t know if there is a workaround for this.

Looking at this ticket in the Azure Functions team (https://github.com/Azure/azure-functions-host/issues/7061) to no avail at the moment.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
kmsrajancommented, Aug 10, 2021

I found the issue. The problem is with Microsoft.Graph latest package(above 3.35.0) in Nuget. Assembly from latest package throws Exception “Could not load file or assembly ‘System.Text.Encodings.Web, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51’. The system cannot find the file specified.”

Thereafter I downgrade the Microsoft.Graph nuget package version to 3.35.0 then it is working as expected 😃

0reactions
kmsrajancommented, Aug 9, 2021

I have this problem too. If run the code in console then every thing working as expected but if I deploy in function app then same exception is comming. Any workaourd to get out of this is much appreciated.

Code:

`IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder .Create(clientId) .WithTenantId(tenantId) .WithRedirectUri(redirectUri) .WithClientSecret(clientSecret) // or .WithCertificate(certificate) .Build();

        ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
        GraphServiceClient graphClient = new GraphServiceClient(authProvider);

        try
        {
            var site1 =await graphClient.Sites["statoilintegrationtest.sharepoint.com:"].Sites["assetrequest"]
           .Request()
           .GetAsync();
            Console.WriteLine(site1.DisplayName);
        }
        catch (ServiceException ex)
        {
            Console.WriteLine("Requested site is not present");
        }`

Console application result: Site details as expected

Function App result :- Exception message: Could not load file or assembly ‘System.Text.Encodings.Web, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51’. The system cannot find the file specified.

Exception Source: System.Text.Json

StackTrace:
at System.Text.Json.JsonPropertyInfo.DeterminePropertyName() at System.Text.Json.JsonPropertyInfo.GetPolicies(Nullable1 ignoreCondition, Nullable1 parentTypeNumberHandling, Boolean defaultValueIsNull) at System.Text.Json.JsonPropertyInfo1.Initialize(Type parentClassType, Type declaredPropertyType, Type runtimePropertyType, ClassType runtimeClassType, MemberInfo memberInfo, JsonConverter converter, Nullable1 ignoreCondition, Nullable1 parentTypeNumberHandling, JsonSerializerOptions options) at System.Text.Json.JsonClassInfo.CreateProperty(Type declaredPropertyType, Type runtimePropertyType, MemberInfo memberInfo, Type parentClassType, JsonConverter converter, JsonSerializerOptions options, Nullable1 parentTypeNumberHandling, Nullable1 ignoreCondition) at System.Text.Json.JsonClassInfo.CreatePropertyInfoForClassInfo(Type declaredPropertyType, Type runtimePropertyType, JsonConverter converter, JsonSerializerOptions options) at System.Text.Json.JsonClassInfo..ctor(Type type, JsonSerializerOptions options) at System.Text.Json.JsonSerializerOptions.GetOrAddClass(Type type) at System.Text.Json.JsonSerializerOptions.GetOrAddClassForRootType(Type type) at System.Text.Json.ReadStack.Initialize(Type type, JsonSerializerOptions options, Boolean supportContinuation) at System.Text.Json.JsonSerializer.<ReadAsync>d__201.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Threading.Tasks.ValueTask1.get_Result() at System.Runtime.CompilerServices.ValueTaskAwaiter1.GetResult() at Microsoft.Graph.Serializer.DeserializeObject[T](Stream stream) at Microsoft.Graph.ResponseHandler.<HandleResponse>d__21.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Microsoft.Graph.BaseRequest.<SendAsync>d__341.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at Microsoft.Graph.SiteRequest.<GetAsync>d__5.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at SiteCheck.Function1.<Run>d__0.MoveNext() in C:\Appl\SiteCheck\Function1.cs:line 54

Read more comments on GitHub >

github_iconTop Results From Across the Web

Could not load file or assembly 'System.Text.Encoding. ...
The root cause of this problem is that the Azure Functions SDK deletes any assembly with a matching name without considering the assembly ......
Read more >
Azure Functions - Could not load file or assembly System. ...
This post is about how to fix the exception Could not load file or assembly System. Text. Encoding. CodePages when you're using any...
Read more >
EPPlus: System.Text.Encoding.CodePages.dll not found in ...
The ExcelPackage instanciation will raise the following error: "Could not load file or assembly 'System.Text.Encoding.CodePages, Version=5.0.0.0 ...
Read more >
Azure Functions How to fix Could not load file or assembly ...
I am making an Azure Durable Functions application that uses Microsoft.IdentityModel.Tokens. When this chunk of code runs I am getting the ...
Read more >
Azure Function and Entity Framework Sql Server error
I found this error to be reproducible. Create a new HTTP Trigger Azure Function, .NET 6. Add the below class to create a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found