Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EPPlus: System.Text.Encoding.CodePages.dll not found in .Net Core 3.1 Azure function

Using VS2019 I have generated an Azure Function project. The Azure function is triggered by an HTTP request to convert CSV content to an XLSX file using EPPlus.

The code is really really simple:

[FunctionName("Function1")]
public static async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log)
{
    ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
    // line below will raise "System.Text.Encoding.CodePages.dll" not found exception
    ExcelPackage xcel = new ExcelPackage();
// ... some other code
}

The ExcelPackage instanciation will raise the following error: "Could not load file or assembly 'System.Text.Encoding.CodePages, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified." EPPlus is relying System.Text.Encoding.CodePages 5.0.0.0 but looks like .Net core 3.1 is relying on a 4.x version.

The workaround I thought and tested successfully was just to copy System.Text.Encoding.CodePages.dll in the bin folder of my solution. But very unsatisfactory because every time I am rebuilding the solution, I have to deploy the System.Text.Encoding.CodePage.dll manually.

I have tried different things I read about binding redirect but unsuccessfully. Not very surprising as binding redirect is more .net framework related.

This drives me nuts.

like image 619
L. Herveleu Avatar asked Oct 15 '22 23:10

L. Herveleu


2 Answers

The ExcelPackage instanciation will raise the following error: "Could not load file or assembly 'System.Text.Encoding.CodePages, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified." EPPlus is relying System.Text.Encoding.CodePages 5.0.0.0 but looks like .Net core 3.1 is relying on a 4.x version.

This is no matter with the version of the System.Text.Encoding.CodePages package. I can explain this situation to you(In fact, it is related to the after-build operation of azure function tools in VS 2019.).

In fact, the System.Text.Encoding.CodePage.dll file has already been copied to the .\bin\Release\netcoreapp3.1\bin folder after the azure function app is built. But, azure function tools did an operation, it removed many files from .\bin\Release\netcoreapp3.1\bin after the azure function app is built.

The workaround I thought and tested successfully was just to copy System.Text.Encoding.CodePages.dll in the bin folder of my solution. But very unsatisfactory because every time I am rebuilding the solution, I have to deploy the System.Text.Encoding.CodePage.dll manually.

You can try to use the 'build' operation instead of the 're-build' operation. 'build' operation will not remove the System.Text.Encoding.CodePage.dll after you put it in the bin folder.

It's not your fault, I think this is a design error of the azure function tool. If the delete operation is not automatically performed after the build operation, no problems will occur. Hope my answer can answer your doubts.

And you can also give a feedback of this problem.

(I think the code you don't need, so I won’t post it. I have do a test, works fine.)

like image 101
Cindy Pau Avatar answered Oct 18 '22 15:10

Cindy Pau


In your Visual Studio Solution Explorer, right-click Azure Functions Project and click on "Edit Project File". Then, add an ItemGroup to preserve System.Text.Encoding.CodePages.dll dependency, like this:

<ItemGroup>
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.13" />
    <PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
    <FunctionsPreservedDependencies Include="System.Text.Encoding.CodePages.dll" />
</ItemGroup>

Be happy!

like image 41
Bruno Leitão Avatar answered Oct 18 '22 13:10

Bruno Leitão