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

3

6

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.

Denture answered 20/12, 2020 at 0:40 Comment(1)
It's not your fault, you can have a look at my answer. And maybe you need to give feedback to azure function program teams.Italianate
I
4

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.)

Italianate answered 21/12, 2020 at 11:35 Comment(4)
Thanks for this explanation. Actually I saw in the build detailed output that the System.Text.Encoding.CodePage.dll file was explicitely copied into my local bin folder and that at the end of the build process the file was not more there. When publishing to Azure from VS2019 it is performing a re-build. So still copying (uploading I should say) the missing System.Text.Encoding.CodePage.dll in the app service bin folder in Azure. I will provide feed back as you have suggested.Denture
@L.Herveleu You can use build instead of rebuild.Italianate
@L.Herveleu Hi, if my answer answered your doubts, can you mark it as the answer? This will helps other people who are troubled by this problem. Thanks.:)Italianate
Thanks a lot, I copied "System.Text.Encoding.CodePage.dll" into my project's "ProfileQueueTriggerFunction\bin\Debug\netcoreapp3.1\bin" folder and it solved my problemArrowood
G
4

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!

Gridley answered 12/8, 2021 at 23:0 Comment(0)
M
0

I had the same problem while trying a similar approach. The solution was "quite simple" and a bit different than the answer Bowman gave.

System.Text.Encoding.CodePages, Version=5.0.0.0 is a package made for .NET5 framework. It references:

Microsoft.NETCore.Platforms (>= 5.0.0)
System.Runtime.CompilerServices.Unsafe (>= 5.0.0)

I think the package is not fully compatible with .NetCore 3.1, as apparently it says.

The solution I found is quite simple, just reference the version 4.71 witch supports the runtime used by Azure Functions on NetCore 3.1 (not NET5).

Malcah answered 14/7, 2021 at 12:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.