Azure functions: Could not load file or assembly System.Text.Encoding.CodePages
See original GitHub issueBug 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
- [Create an Azure Functions project from scratch (did this with VS2019)]
- [Create a function]
- [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:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
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 😃
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();
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(Nullable
1 ignoreCondition, Nullable
1 parentTypeNumberHandling, Boolean defaultValueIsNull) at System.Text.Json.JsonPropertyInfo1.Initialize(Type parentClassType, Type declaredPropertyType, Type runtimePropertyType, ClassType runtimeClassType, MemberInfo memberInfo, JsonConverter converter, Nullable
1 ignoreCondition, Nullable1 parentTypeNumberHandling, JsonSerializerOptions options) at System.Text.Json.JsonClassInfo.CreateProperty(Type declaredPropertyType, Type runtimePropertyType, MemberInfo memberInfo, Type parentClassType, JsonConverter converter, JsonSerializerOptions options, Nullable
1 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__20
1.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.ValueTaskAwaiter
1.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.TaskAwaiter
1.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.ConfiguredTaskAwaitable
1.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